Skip to content

Commit

Permalink
Move backends behind crate features
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Aug 25, 2024
1 parent 9b07ece commit aa1787d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
# Would be nice to have https://github.com/rust-lang/cargo/issues/1197
default = ["adwaita", "swiftui"]
adwaita = ["dep:nuit-bridge-adwaita"]
swiftui = ["dep:nuit-bridge-swiftui"]

[dependencies]
nuit-derive.workspace = true
nuit-core.workspace = true
# TODO: Scope this under not(apple) to avoid pulling it in on macOS?
# Would be nice to have https://github.com/rust-lang/cargo/issues/1197
nuit-bridge-adwaita.workspace = true

[target.'cfg(target_vendor = "apple")'.dependencies]
nuit-bridge-swiftui.workspace = true
nuit-bridge-adwaita = { workspace = true, optional = true }
nuit-bridge-swiftui = { workspace = true, optional = true }

[workspace]
members = [
Expand Down
1 change: 1 addition & 0 deletions nuit-bridge-swiftui/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use nuit_core::CRoot;

#[cfg(target_vendor = "apple")]
extern "C" {
pub fn run_app(root: *const CRoot);
}
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ pub use nuit_core::*;
pub use nuit_derive::*;

impl Default for Backend {
#[allow(unreachable_code)]
fn default() -> Self {
#[cfg(target_vendor = "apple")]
#[cfg(feature = "swiftui")]
return Backend::SwiftUI;
#[cfg(not(target_vendor = "apple"))]
#[cfg(feature = "adwaita")]
return Backend::Adwaita;
panic!("A backend must be enabled via Nuit's crate features!");
}
}

Expand All @@ -24,15 +26,19 @@ pub fn run_app<T>(config: impl Into<Config<T>>) where T: View + 'static {
let root = Root::new(view);

match backend {
#[cfg(feature = "swiftui")]
Backend::SwiftUI => {
let c_root = CRoot::from(Box::new(root));
#[cfg(target_vendor = "apple")]
unsafe { nuit_bridge_swiftui::run_app(&c_root); }
#[cfg(not(target_vendor = "apple"))]
panic!("SwiftUI is not supported outside of Apple platforms!")
}
#[cfg(feature = "adwaita")]
Backend::Adwaita => {
nuit_bridge_adwaita::run_app(root);
}
#[allow(unreachable_patterns)]
_ => panic!("The backend {backend:?} must be enabled via Nuit's crate features!"),
}
}

0 comments on commit aa1787d

Please sign in to comment.