Skip to content

Commit

Permalink
Playtime: Prefer OS distinction at runtime over compile time, where p…
Browse files Browse the repository at this point in the history
…ossible

in order to avoid too many compile variations
  • Loading branch information
helgoboss committed Nov 16, 2023
1 parent 223365c commit 9c4e489
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
24 changes: 14 additions & 10 deletions main/src/infrastructure/ui/app/app_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ pub trait AppInstance: Debug {
fn notify_app_is_ready(&mut self, callback: AppCallback);
}

pub fn create_app_instance(session: WeakSession) -> impl AppInstance {
pub fn create_shared_app_instance(session: WeakSession) -> SharedAppInstance {
fn share(value: impl AppInstance + 'static) -> SharedAppInstance {
Rc::new(RefCell::new(value))
}
// I was experimenting with 2 different ways of embedding the app GUI into REAPER:
//
// - Parented mode: We create a new SWELL window on ReaLearn side (HWND on Windows, NSView on
Expand All @@ -44,17 +47,15 @@ pub fn create_app_instance(session: WeakSession) -> impl AppInstance {
// 3. We stop sending events when the app window is hidden, not wasting resources when the
// app is not shown anyway. (However, with just a bit more effort, we could implement this
// for standalone mode as well.)
#[cfg(target_os = "windows")]
{
if cfg!(target_os = "windows") {
// On Windows, parented mode works wonderfully. It needs some tricks (see View
// implementation) but it's worth it. That's why we use parented mode on Windows.
let app_panel = AppPanel::new(session);
ParentedAppInstance {
let instance = ParentedAppInstance {
panel: SharedView::new(app_panel),
}
}
#[cfg(target_os = "macos")]
{
};
share(instance)
} else if cfg!(target_os = "macos") {
// On macOS, parented mode is possible only by using Cocoa child windows (see app side
// embedding docs). This means that the app doesn't really render itself in the NSView
// provided by ReaLearn but places an NSWindow on top of the NSWindow provided by ReaLearn.
Expand All @@ -64,10 +65,13 @@ pub fn create_app_instance(session: WeakSession) -> impl AppInstance {
// This could be solved on app side by navigating up the child/parent window chain, but
// it's not something I want to do now as long as we don't support docking anyway.
// Therefore: Standalone mode on macOS!
StandaloneAppInstance {
let instance = StandaloneAppInstance {
session,
running_state: None,
}
};
share(instance)
} else {
panic!("OS not supported yet");
}
}

Expand Down
60 changes: 28 additions & 32 deletions main/src/infrastructure/ui/app/app_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,25 @@ impl Drop for AppLibrary {

impl AppLibrary {
pub fn load(app_base_dir: PathBuf) -> Result<Self> {
let (main_library, dependencies) = {
#[cfg(target_os = "windows")]
{
(
"playtime.dll",
[
// Important: This must be the first. Because below plug-in libraries
// depend on it.
"flutter_windows.dll",
// The rest can have an arbitrary order.
"desktop_drop_plugin.dll",
"native_context_menu_plugin.dll",
"screen_retriever_plugin.dll",
"url_launcher_windows_plugin.dll",
"window_manager_plugin.dll",
"pointer_lock_plugin.dll",
],
)
}
#[cfg(target_os = "macos")]
{
(
let (main_library, dependencies) = if cfg!(target_os = "windows") {
(
"playtime.dll",
[
// Important: This must be the first. Because below plug-in libraries
// depend on it.
"flutter_windows.dll",
// The rest can have an arbitrary order.
"desktop_drop_plugin.dll",
"native_context_menu_plugin.dll",
"screen_retriever_plugin.dll",
"url_launcher_windows_plugin.dll",
"window_manager_plugin.dll",
"pointer_lock_plugin.dll",
]
.as_slice(),
)
} else if cfg!(target_os = "macos") {
(
"Contents/MacOS/playtime",
[
// Important: This must be the first. Because below plug-in libraries
Expand All @@ -74,19 +71,18 @@ impl AppLibrary {
"Contents/Frameworks/url_launcher_macos.framework/url_launcher_macos",
"Contents/Frameworks/window_manager.framework/window_manager",
"Contents/Frameworks/pointer_lock.framework/pointer_lock",
],
)
}
#[cfg(target_os = "linux")]
{
(
"playtime.so",
["flutter_linux.so", "url_launcher_linux_plugin.so"],
].as_slice(),
)
}
} else if cfg!(target_os = "linux") {
(
"playtime.so",
["flutter_linux.so", "url_launcher_linux_plugin.so"].as_slice(),
)
} else {
bail!("OS not supported");
};
let loaded_dependencies: Result<Vec<Library>> = dependencies
.into_iter()
.iter()
.map(|dep| load_library(&app_base_dir.join(dep)))
.collect();
let library = AppLibrary {
Expand Down
4 changes: 1 addition & 3 deletions main/src/infrastructure/ui/independent_panel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ impl IndependentPanelManager {
main_panel,
mapping_panels: Default::default(),
#[cfg(feature = "playtime")]
app_instance: std::rc::Rc::new(std::cell::RefCell::new(
crate::infrastructure::ui::create_app_instance(session.clone()),
)),
app_instance: crate::infrastructure::ui::create_shared_app_instance(session.clone()),
message_panel: SharedView::new(SessionMessagePanel::new(session)),
}
}
Expand Down

0 comments on commit 9c4e489

Please sign in to comment.