Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rfd::AsyncFileDialog;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
Expand All @@ -12,22 +11,20 @@ use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::event_loop::ControlFlow;
use winit::window::Window;
use winit::window::WindowId;

use crate::cef;
use crate::consts::CEF_MESSAGE_LOOP_MAX_ITERATIONS;
use crate::event::{AppEvent, AppEventScheduler};
use crate::native_window;
use crate::persist::PersistentData;
use crate::render::GraphicsState;
use crate::window::Window;
use graphite_desktop_wrapper::messages::{DesktopFrontendMessage, DesktopWrapperMessage, Platform};
use graphite_desktop_wrapper::{DesktopWrapper, NodeGraphExecutionResult, WgpuContext, serialize_frontend_messages};

pub(crate) struct App {
cef_context: Box<dyn cef::CefContext>,
window: Option<Arc<dyn Window>>,
native_window: native_window::NativeWindowHandle,
window: Option<Window>,
cef_schedule: Option<Instant>,
cef_window_size_sender: Sender<cef::WindowSize>,
graphics_state: Option<GraphicsState>,
Expand Down Expand Up @@ -82,7 +79,6 @@ impl App {
web_communication_initialized: false,
web_communication_startup_buffer: Vec::new(),
persistent_data,
native_window: Default::default(),
launch_documents,
}
}
Expand Down Expand Up @@ -173,18 +169,17 @@ impl App {
}
DesktopFrontendMessage::MinimizeWindow => {
if let Some(window) = &self.window {
window.set_minimized(true);
window.minimize();
}
}
DesktopFrontendMessage::MaximizeWindow => {
if let Some(window) = &self.window {
let maximized = !window.is_maximized();
window.set_maximized(maximized);
window.toggle_maximize();
}
}
DesktopFrontendMessage::DragWindow => {
if let Some(window) = &self.window {
let _ = window.drag_window();
let _ = window.start_drag();
}
}
DesktopFrontendMessage::CloseWindow => {
Expand Down Expand Up @@ -348,15 +343,11 @@ impl App {
}
impl ApplicationHandler for App {
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
let window_attributes = self.native_window.build(event_loop);

let window: Arc<dyn Window> = Arc::from(event_loop.create_window(window_attributes).unwrap());

self.native_window.setup(window.as_ref());
let window = Window::new(event_loop);
self.window = Some(window);

let graphics_state = GraphicsState::new(window.clone(), self.wgpu_context.clone());
let graphics_state = GraphicsState::new(self.window.as_ref().unwrap(), self.wgpu_context.clone());

self.window = Some(window);
self.graphics_state = Some(graphics_state);

tracing::info!("Winit window created and ready");
Expand Down Expand Up @@ -397,7 +388,7 @@ impl ApplicationHandler for App {
let Some(ref mut graphics_state) = self.graphics_state else { return };
// Only rerender once we have a new UI texture to display
if let Some(window) = &self.window {
match graphics_state.render(window.as_ref()) {
match graphics_state.render(window) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => {
tracing::warn!("lost surface");
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod cef;
mod cli;
mod dirs;
mod event;
mod native_window;
mod persist;
mod render;
mod window;

mod gpu_context;

Expand Down
71 changes: 0 additions & 71 deletions desktop/src/native_window.rs

This file was deleted.

10 changes: 4 additions & 6 deletions desktop/src/render/graphics_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;
use winit::window::Window;
use crate::window::Window;

use graphite_desktop_wrapper::{Color, WgpuContext, WgpuExecutor};

Expand All @@ -24,10 +23,9 @@ pub(crate) struct GraphicsState {
}

impl GraphicsState {
pub(crate) fn new(window: Arc<dyn Window>, context: WgpuContext) -> Self {
pub(crate) fn new(window: &Window, context: WgpuContext) -> Self {
let size = window.surface_size();

let surface = context.instance.create_surface(window).unwrap();
let surface = window.create_surface(context.instance.clone());

let surface_caps = surface.get_capabilities(&context.adapter);
let surface_format = surface_caps.formats.iter().find(|f| f.is_srgb()).copied().unwrap_or(surface_caps.formats[0]);
Expand Down Expand Up @@ -232,7 +230,7 @@ impl GraphicsState {
self.bind_overlays_texture(texture);
}

pub(crate) fn render(&mut self, window: &dyn Window) -> Result<(), wgpu::SurfaceError> {
pub(crate) fn render(&mut self, window: &Window) -> Result<(), wgpu::SurfaceError> {
if let Some(scene) = self.overlays_scene.take() {
self.render_overlays(scene);
}
Expand Down
87 changes: 87 additions & 0 deletions desktop/src/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::sync::Arc;
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window as WinitWindow, WindowAttributes};

use crate::consts::APP_NAME;

pub(crate) trait NativeWindow {
fn configure(attributes: WindowAttributes, event_loop: &dyn ActiveEventLoop) -> WindowAttributes;
fn new(window: &dyn WinitWindow) -> Self;
}

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux as native;

#[cfg(target_os = "macos")]
mod mac;
#[cfg(target_os = "macos")]
use mac as native;

#[cfg(target_os = "windows")]
mod win;
#[cfg(target_os = "windows")]
use win as native;

pub(crate) struct Window {
winit_window: Arc<dyn winit::window::Window>,
#[allow(dead_code)]
native_handle: native::NativeWindowImpl,
}

impl Window {
pub(crate) fn new(event_loop: &dyn ActiveEventLoop) -> Self {
let mut attributes = WindowAttributes::default()
.with_title(APP_NAME)
.with_min_surface_size(winit::dpi::LogicalSize::new(400, 300))
.with_surface_size(winit::dpi::LogicalSize::new(1200, 800))
.with_resizable(true)
.with_theme(Some(winit::window::Theme::Dark));

attributes = native::NativeWindowImpl::configure(attributes, event_loop);

let winit_window = event_loop.create_window(attributes).unwrap();
let native_handle = native::NativeWindowImpl::new(winit_window.as_ref());
Self {
winit_window: winit_window.into(),
native_handle,
}
}

pub(crate) fn request_redraw(&self) {
self.winit_window.request_redraw();
}

pub(crate) fn create_surface(&self, instance: Arc<wgpu::Instance>) -> wgpu::Surface<'static> {
instance.create_surface(self.winit_window.clone()).unwrap()
}

pub(crate) fn pre_present_notify(&self) {
self.winit_window.pre_present_notify();
}

pub(crate) fn surface_size(&self) -> winit::dpi::PhysicalSize<u32> {
self.winit_window.surface_size()
}

pub(crate) fn minimize(&self) {
self.winit_window.set_minimized(true);
}

pub(crate) fn toggle_maximize(&self) {
self.winit_window.set_maximized(!self.winit_window.is_maximized());
}

pub(crate) fn is_maximized(&self) -> bool {
self.winit_window.is_maximized()
}

pub(crate) fn start_drag(&self) {
let _ = self.winit_window.drag_window();
}

pub(crate) fn set_cursor(&self, cursor: winit::cursor::Cursor) {
self.winit_window.set_cursor(cursor);
}
}
27 changes: 27 additions & 0 deletions desktop/src/window/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use winit::event_loop::ActiveEventLoop;
use winit::platform::wayland::ActiveEventLoopExtWayland;
use winit::platform::wayland::WindowAttributesWayland;
use winit::platform::x11::WindowAttributesX11;
use winit::window::{Window, WindowAttributes};

use crate::consts::{APP_ID, APP_NAME};

use super::NativeWindow;

pub(super) struct NativeWindowImpl {}

impl NativeWindow for NativeWindowImpl {
fn configure(attributes: WindowAttributes, event_loop: &dyn ActiveEventLoop) -> WindowAttributes {
if event_loop.is_wayland() {
let wayland_attributes = WindowAttributesWayland::default().with_name(APP_ID, "").with_prefer_csd(true);
attributes.with_platform_attributes(Box::new(wayland_attributes))
} else {
let x11_attributes = WindowAttributesX11::default().with_name(APP_ID, APP_NAME);
attributes.with_platform_attributes(Box::new(x11_attributes))
}
}

fn new(_window: &dyn Window) -> Self {
NativeWindowImpl {}
}
}
20 changes: 20 additions & 0 deletions desktop/src/window/mac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window, WindowAttributes};

use super::NativeWindow;

pub(super) struct NativeWindowImpl {}

impl NativeWindow for NativeWindowImpl {
fn configure(attributes: WindowAttributes, _event_loop: &dyn ActiveEventLoop) -> WindowAttributes {
let mac_window = winit::platform::macos::WindowAttributesMacOS::default()
.with_titlebar_transparent(true)
.with_fullsize_content_view(true)
.with_title_hidden(true);
attributes.with_platform_attributes(Box::new(mac_window))
}

fn new(_window: &dyn Window) -> Self {
NativeWindowImpl {}
}
}
32 changes: 32 additions & 0 deletions desktop/src/window/win.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window, WindowAttributes};

use super::NativeWindow;

pub(super) struct NativeWindowImpl {
native_handle: native_handle::NativeWindowHandle,
}

impl NativeWindow for NativeWindowImpl {
fn configure(attributes: WindowAttributes, _event_loop: &dyn ActiveEventLoop) -> WindowAttributes {
if let Ok(win_icon) = winit::platform::windows::WinIcon::from_resource(1, None) {
let icon = winit::icon::Icon(std::sync::Arc::new(win_icon));
attributes.with_window_icon(Some(icon))
} else {
attributes
}
}

fn new(window: &dyn Window) -> Self {
let native_handle = native_handle::NativeWindowHandle::new(window);
NativeWindowImpl { native_handle }
}
}

impl Drop for NativeWindowImpl {
fn drop(&mut self) {
self.native_handle.destroy();
}
}

mod native_handle;
Loading