Skip to content

Commit 24563a6

Browse files
Replace globals with editor enviroment
1 parent d2bc0fd commit 24563a6

27 files changed

Lines changed: 164 additions & 198 deletions

desktop/src/app.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rand::Rng;
12
use rfd::AsyncFileDialog;
23
use std::fs;
34
use std::path::PathBuf;
@@ -16,7 +17,7 @@ use crate::event::{AppEvent, AppEventScheduler};
1617
use crate::persist::PersistentData;
1718
use crate::render::{RenderError, RenderState};
1819
use crate::window::Window;
19-
use crate::wrapper::messages::{DesktopFrontendMessage, DesktopWrapperMessage, InputMessage, MouseKeys, MouseState, Platform};
20+
use crate::wrapper::messages::{DesktopFrontendMessage, DesktopWrapperMessage, InputMessage, MouseKeys, MouseState};
2021
use crate::wrapper::{DesktopWrapper, NodeGraphExecutionResult, WgpuContext, serialize_frontend_messages};
2122

2223
pub(crate) struct App {
@@ -46,7 +47,8 @@ pub(crate) struct App {
4647
}
4748

4849
impl App {
49-
pub(crate) fn init() {
50+
pub(crate) fn init(wgpu_context: &WgpuContext) {
51+
DesktopWrapper::init(wgpu_context, rand::rng().random());
5052
Window::init();
5153
}
5254

@@ -476,16 +478,6 @@ impl ApplicationHandler for App {
476478
}
477479

478480
self.resize();
479-
480-
self.desktop_wrapper.init(self.wgpu_context.clone());
481-
482-
#[cfg(target_os = "windows")]
483-
let platform = Platform::Windows;
484-
#[cfg(target_os = "macos")]
485-
let platform = Platform::Mac;
486-
#[cfg(target_os = "linux")]
487-
let platform = Platform::Linux;
488-
self.dispatch_desktop_wrapper_message(DesktopWrapperMessage::UpdatePlatform(platform));
489481
}
490482

491483
fn proxy_wake_up(&mut self, event_loop: &dyn ActiveEventLoop) {

desktop/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ pub fn start() {
6666
}
6767
};
6868

69-
App::init();
70-
7169
let wgpu_context = futures::executor::block_on(gpu_context::create_wgpu_context());
7270

71+
App::init(&wgpu_context);
72+
7373
let event_loop = EventLoop::new().unwrap();
7474
let (app_event_sender, app_event_receiver) = std::sync::mpsc::channel();
7575
let app_event_scheduler = event_loop.create_app_event_scheduler(app_event_sender);
@@ -78,9 +78,9 @@ pub fn start() {
7878

7979
let cef_handler = cef::CefHandler::new(wgpu_context.clone(), app_event_scheduler.clone(), cef_view_info_receiver);
8080
let cef_context = match cef_context_builder.initialize(cef_handler, cli.disable_ui_acceleration) {
81-
Ok(c) => {
81+
Ok(context) => {
8282
tracing::info!("CEF initialized successfully");
83-
c
83+
context
8484
}
8585
Err(cef::InitError::AlreadyRunning) => {
8686
tracing::error!("Another instance is already running, Exiting.");

desktop/wrapper/src/handle_desktop_wrapper_message.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use graphene_std::Color;
22
use graphene_std::raster::Image;
3-
use graphite_editor::messages::app_window::app_window_message_handler::AppWindowPlatform;
43
use graphite_editor::messages::clipboard::utility_types::ClipboardContentRaw;
54
use graphite_editor::messages::prelude::*;
65

76
use super::DesktopWrapperMessageDispatcher;
8-
use super::messages::{DesktopFrontendMessage, DesktopWrapperMessage, EditorMessage, OpenFileDialogContext, Platform, SaveFileDialogContext};
7+
use super::messages::{DesktopFrontendMessage, DesktopWrapperMessage, EditorMessage, OpenFileDialogContext, SaveFileDialogContext};
98

109
pub(super) fn handle_desktop_wrapper_message(dispatcher: &mut DesktopWrapperMessageDispatcher, message: DesktopWrapperMessage) {
1110
match message {
@@ -111,15 +110,6 @@ pub(super) fn handle_desktop_wrapper_message(dispatcher: &mut DesktopWrapperMess
111110
dispatcher.queue_editor_message(message);
112111
}
113112
DesktopWrapperMessage::PollNodeGraphEvaluation => dispatcher.poll_node_graph_evaluation(),
114-
DesktopWrapperMessage::UpdatePlatform(platform) => {
115-
let platform = match platform {
116-
Platform::Windows => AppWindowPlatform::Windows,
117-
Platform::Mac => AppWindowPlatform::Mac,
118-
Platform::Linux => AppWindowPlatform::Linux,
119-
};
120-
let message = AppWindowMessage::UpdatePlatform { platform };
121-
dispatcher.queue_editor_message(message);
122-
}
123113
DesktopWrapperMessage::UpdateMaximized { maximized } => {
124114
let message = FrontendMessage::UpdateMaximized { maximized };
125115
dispatcher.queue_editor_message(message);

desktop/wrapper/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use graph_craft::wasm_application_io::WasmApplicationIo;
2-
use graphite_editor::application::Editor;
2+
use graphite_editor::application::{Editor, Environment, Host, Platform};
33
use graphite_editor::messages::prelude::{FrontendMessage, Message};
44

55
// TODO: Remove usage of this reexport in desktop create and remove this line
@@ -27,15 +27,24 @@ pub struct DesktopWrapper {
2727
}
2828

2929
impl DesktopWrapper {
30-
pub fn new() -> Self {
31-
Self { editor: Editor::new() }
32-
}
30+
pub fn init(wgpu_context: &WgpuContext, uuid_random_seed: u64) {
31+
#[cfg(target_os = "windows")]
32+
let host = Host::Windows;
33+
#[cfg(target_os = "macos")]
34+
let host = Host::Mac;
35+
#[cfg(target_os = "linux")]
36+
let host = Host::Linux;
37+
let env = Environment { platform: Platform::Desktop, host };
38+
Editor::init(env, uuid_random_seed);
3339

34-
pub fn init(&self, wgpu_context: WgpuContext) {
35-
let application_io = WasmApplicationIo::new_with_context(wgpu_context);
40+
let application_io = WasmApplicationIo::new_with_context(wgpu_context.clone());
3641
futures::executor::block_on(graphite_editor::node_graph_executor::replace_application_io(application_io));
3742
}
3843

44+
pub fn new() -> Self {
45+
Self { editor: Editor::new() }
46+
}
47+
3948
pub fn dispatch(&mut self, message: DesktopWrapperMessage) -> Vec<DesktopFrontendMessage> {
4049
let mut executor = DesktopWrapperMessageDispatcher::new(&mut self.editor);
4150
executor.queue_desktop_wrapper_message(message);

desktop/wrapper/src/messages.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ pub enum DesktopWrapperMessage {
109109
content: Vec<u8>,
110110
},
111111
PollNodeGraphEvaluation,
112-
UpdatePlatform(Platform),
113112
UpdateMaximized {
114113
maximized: bool,
115114
},
@@ -163,12 +162,6 @@ pub enum SaveFileDialogContext {
163162
File { content: Vec<u8> },
164163
}
165164

166-
pub enum Platform {
167-
Windows,
168-
Mac,
169-
Linux,
170-
}
171-
172165
pub enum MenuItem {
173166
Action {
174167
id: String,

editor/src/application.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::OnceLock;
2+
13
use crate::dispatcher::Dispatcher;
24
use crate::messages::prelude::*;
35
pub use graphene_std::uuid::*;
@@ -8,8 +10,12 @@ pub struct Editor {
810
}
911

1012
impl Editor {
11-
/// Construct the editor.
12-
/// Remember to provide a random seed with `editor::set_uuid_seed(seed)` before any editors can be used.
13+
pub fn init(enviroment: Environment, uuid_random_seed: u64) {
14+
ENVIROMENT.set(enviroment).expect("Editor shoud only be initialized once");
15+
graphene_std::uuid::init_rng(uuid_random_seed);
16+
}
17+
18+
/// `Editor::init` must be called before this function
1319
pub fn new() -> Self {
1420
Self { dispatcher: Dispatcher::new() }
1521
}
@@ -31,13 +37,57 @@ impl Editor {
3137
self.dispatcher.poll_node_graph_evaluation(responses)
3238
}
3339
}
34-
3540
impl Default for Editor {
3641
fn default() -> Self {
3742
Self::new()
3843
}
3944
}
4045

46+
static ENVIROMENT: OnceLock<Environment> = OnceLock::new();
47+
impl Editor {
48+
pub fn environment() -> &'static Environment {
49+
ENVIROMENT.get().expect("Editor environment accessed before initialization")
50+
}
51+
}
52+
53+
#[derive(Clone, Copy, Debug)]
54+
pub struct Environment {
55+
pub platform: Platform,
56+
pub host: Host,
57+
}
58+
#[derive(Clone, Copy, Debug)]
59+
pub enum Platform {
60+
Desktop,
61+
Web,
62+
}
63+
#[derive(Clone, Copy, Debug)]
64+
pub enum Host {
65+
Linux,
66+
Mac,
67+
Windows,
68+
Unknown,
69+
}
70+
impl Environment {
71+
pub fn is_desktop(&self) -> bool {
72+
matches!(self.platform, Platform::Desktop)
73+
}
74+
pub fn is_web(&self) -> bool {
75+
matches!(self.platform, Platform::Web)
76+
}
77+
pub fn is_linux(&self) -> bool {
78+
matches!(self.host, Host::Linux)
79+
}
80+
pub fn is_mac(&self) -> bool {
81+
matches!(self.host, Host::Mac)
82+
}
83+
pub fn is_windows(&self) -> bool {
84+
matches!(self.host, Host::Windows)
85+
}
86+
pub fn is_unknown_host(&self) -> bool {
87+
matches!(self.host, Host::Unknown)
88+
}
89+
}
90+
4191
pub const GRAPHITE_RELEASE_SERIES: &str = env!("GRAPHITE_RELEASE_SERIES");
4292
pub const GRAPHITE_GIT_COMMIT_BRANCH: Option<&str> = option_env!("GRAPHITE_GIT_COMMIT_BRANCH");
4393
pub const GRAPHITE_GIT_COMMIT_HASH: &str = env!("GRAPHITE_GIT_COMMIT_HASH");

editor/src/dispatcher.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::application::Editor;
12
use crate::messages::debug::utility_types::MessageLoggingVerbosity;
23
use crate::messages::defer::DeferMessageContext;
34
use crate::messages::dialog::DialogMessageContext;
@@ -23,7 +24,6 @@ pub struct DispatcherMessageHandlers {
2324
debug_message_handler: DebugMessageHandler,
2425
defer_message_handler: DeferMessageHandler,
2526
dialog_message_handler: DialogMessageHandler,
26-
globals_message_handler: GlobalsMessageHandler,
2727
input_preprocessor_message_handler: InputPreprocessorMessageHandler,
2828
key_mapping_message_handler: KeyMappingMessageHandler,
2929
layout_message_handler: LayoutMessageHandler,
@@ -192,17 +192,12 @@ impl Dispatcher {
192192
self.responses.push(message);
193193
}
194194
}
195-
Message::Globals(message) => {
196-
self.message_handlers.globals_message_handler.process_message(message, &mut queue, ());
197-
}
198195
Message::InputPreprocessor(message) => {
199-
let keyboard_platform = GLOBAL_PLATFORM.get().copied().unwrap_or_default().as_keyboard_platform_layout();
200-
201196
self.message_handlers.input_preprocessor_message_handler.process_message(
202197
message,
203198
&mut queue,
204199
InputPreprocessorMessageContext {
205-
keyboard_platform,
200+
keyboard_platform: Editor::environment().host.into(),
206201
viewport: &self.message_handlers.viewport_message_handler,
207202
},
208203
);
@@ -309,6 +304,10 @@ impl Dispatcher {
309304
Message::Viewport(message) => {
310305
self.message_handlers.viewport_message_handler.process_message(message, &mut queue, ());
311306
}
307+
Message::Init => {
308+
self.handle_message(AppWindowMessage::Init, false);
309+
self.handle_message(PortfolioMessage::Init, false);
310+
}
312311
Message::NoOp => {}
313312
Message::Batched { messages } => {
314313
messages.into_iter().for_each(|message| self.handle_message(message, false));

editor/src/messages/app_window/app_window_message.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use crate::messages::prelude::*;
22

3-
use super::app_window_message_handler::AppWindowPlatform;
4-
53
#[impl_message(Message, AppWindow)]
64
#[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
75
pub enum AppWindowMessage {
8-
UpdatePlatform { platform: AppWindowPlatform },
6+
Init,
97
PointerLock,
108
PointerLockMove { x: f64, y: f64 },
119
Close,

editor/src/messages/app_window/app_window_message_handler.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use crate::messages::app_window::AppWindowMessage;
1+
use crate::application::{Editor, Environment};
22
use crate::messages::prelude::*;
3+
use crate::{application::Host, messages::app_window::AppWindowMessage};
34
use graphite_proc_macros::{ExtractField, message_handler_data};
45

56
#[derive(Debug, Clone, Default, ExtractField)]
6-
pub struct AppWindowMessageHandler {
7-
platform: AppWindowPlatform,
8-
}
7+
pub struct AppWindowMessageHandler {}
98

109
#[message_handler_data]
1110
impl MessageHandler<AppWindowMessage, ()> for AppWindowMessageHandler {
1211
fn process_message(&mut self, message: AppWindowMessage, responses: &mut std::collections::VecDeque<Message>, _: ()) {
1312
match message {
14-
AppWindowMessage::UpdatePlatform { platform } => {
15-
self.platform = platform;
16-
responses.add(FrontendMessage::UpdatePlatform { platform: self.platform });
13+
AppWindowMessage::Init => {
14+
responses.add(FrontendMessage::UpdatePlatform {
15+
platform: Editor::environment().into(),
16+
});
1717
}
1818
AppWindowMessage::PointerLock => {
1919
responses.add(FrontendMessage::WindowPointerLock);
@@ -66,3 +66,16 @@ pub enum AppWindowPlatform {
6666
Mac,
6767
Linux,
6868
}
69+
70+
impl From<&Environment> for AppWindowPlatform {
71+
fn from(environment: &Environment) -> Self {
72+
if environment.is_web() {
73+
return AppWindowPlatform::Web;
74+
}
75+
match environment.host {
76+
Host::Linux => AppWindowPlatform::Linux,
77+
Host::Mac => AppWindowPlatform::Mac,
78+
Host::Windows | Host::Unknown => AppWindowPlatform::Windows,
79+
}
80+
}
81+
}

editor/src/messages/globals/global_variables.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)