From 67c7c8412ec551dafb641555f9c2b47baafa2ebe Mon Sep 17 00:00:00 2001 From: Vitalii Lukyanov Date: Sat, 28 Sep 2024 12:24:10 +0200 Subject: [PATCH 1/3] iced_sessionlock to_session_message macro, similar to to_layer_message macro from iced_layershell --- Cargo.lock | 13 +++++ Cargo.toml | 39 ++++++++------ iced_layershell_macros/Cargo.toml | 10 ++-- iced_sessionlock/Cargo.toml | 2 + iced_sessionlock/README.md | 14 +---- iced_sessionlock/src/lib.rs | 17 +++--- iced_sessionlock_macros/Cargo.toml | 28 ++++++++++ iced_sessionlock_macros/src/lib.rs | 58 +++++++++++++++++++++ iced_sessionlock_macros/tests/macro_test.rs | 13 +++++ 9 files changed, 153 insertions(+), 41 deletions(-) create mode 100644 iced_sessionlock_macros/Cargo.toml create mode 100644 iced_sessionlock_macros/src/lib.rs create mode 100644 iced_sessionlock_macros/tests/macro_test.rs diff --git a/Cargo.lock b/Cargo.lock index 37a6bb7..6e2f758 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1873,6 +1873,7 @@ dependencies = [ "iced_graphics", "iced_renderer", "iced_runtime", + "iced_sessionlock_macros", "log", "sessionlockev", "thiserror", @@ -1880,6 +1881,18 @@ dependencies = [ "window_clipboard", ] +[[package]] +name = "iced_sessionlock_macros" +version = "0.7.2" +dependencies = [ + "darling", + "iced_sessionlock", + "manyhow", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "iced_tiny_skia" version = "0.13.0" diff --git a/Cargo.toml b/Cargo.toml index 20a864e..3ac4301 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,20 +1,21 @@ [workspace] resolver = "2" members = [ - "layershellev", - "iced_layershell", - "iced_layershell_macros", - "iced_sessionlock", - "starcolorkeyboard", - "sessionlockev", - "waycrate_xkbkeycode", - "iced_examples/*", + "layershellev", + "iced_layershell", + "iced_layershell_macros", + "iced_sessionlock", + "iced_sessionlock_macros", + "starcolorkeyboard", + "sessionlockev", + "waycrate_xkbkeycode", + "iced_examples/*", ] [workspace.package] authors = [ - "Decodertalkers ", - "Aakash Sen Sharma ", + "Decodertalkers ", + "Aakash Sen Sharma ", ] edition = "2021" version = "0.7.2" @@ -38,28 +39,28 @@ thiserror = "1.0.63" wayland-client = { version = "0.31.5" } wayland-protocols = { version = "0.32.3", default-features = false, features = [ - "unstable", - "staging", - "client", + "unstable", + "staging", + "client", ] } wayland-cursor = "0.31.5" wayland-protocols-wlr = { version = "0.3.3", default-features = false, features = [ - "client", + "client", ] } wayland-protocols-misc = { version = "0.3.3", features = ["client"] } wayland-backend = { version = "0.3.6", features = ["client_system"] } sctk = { package = "smithay-client-toolkit", version = "0.18.1", features = [ - "calloop", + "calloop", ] } calloop = "0.14.0" rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = [ - "std", + "std", ] } rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"] } @@ -81,3 +82,9 @@ memmap2 = "0.9.4" tracing = "0.1.40" futures = "0.3.30" + +darling = { version = "0.20.10", features = ["suggestions"] } +manyhow = { version = "0.11.4", features = ["darling"] } +proc-macro2 = "1.0.86" +quote = "1.0.37" +syn = { version = "2.0.77", features = ["full"] } diff --git a/iced_layershell_macros/Cargo.toml b/iced_layershell_macros/Cargo.toml index 15eb68d..245f0ac 100644 --- a/iced_layershell_macros/Cargo.toml +++ b/iced_layershell_macros/Cargo.toml @@ -18,11 +18,11 @@ default = [] diagnostics = ["darling/diagnostics"] [dependencies] -darling = { version = "0.20.10", features = ["suggestions"] } -manyhow = { version = "0.11.4", features = ["darling"] } -proc-macro2 = "1.0.86" -quote = "1.0.37" -syn = { version = "2.0.77", features = ["full"] } +darling.workspace = true +manyhow.workspace = true +proc-macro2.workspace = true +quote.workspace = true +syn.workspace = true [dev-dependencies] iced_layershell.workspace = true diff --git a/iced_sessionlock/Cargo.toml b/iced_sessionlock/Cargo.toml index 87b4da6..68fb617 100644 --- a/iced_sessionlock/Cargo.toml +++ b/iced_sessionlock/Cargo.toml @@ -11,6 +11,8 @@ description = "sessionlock binding for iced" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +iced_sessionlock_macros = { path = "../iced_sessionlock_macros" } + iced.workspace = true iced_renderer.workspace = true iced_runtime.workspace = true diff --git a/iced_sessionlock/README.md b/iced_sessionlock/README.md index 7ad5caa..3fd6c25 100644 --- a/iced_sessionlock/README.md +++ b/iced_sessionlock/README.md @@ -15,6 +15,7 @@ use iced::{event, Alignment, Element, Event, Length, Task as Command, Theme}; use iced_sessionlock::actions::UnLockAction; use iced_sessionlock::settings::Settings; use iced_sessionlock::MultiApplication; +use iced_sessionlock::to_session_message; pub fn main() -> Result<(), iced_sessionlock::Error> { Counter::run(Settings::default()) @@ -25,24 +26,13 @@ struct Counter { text: String, } +#[to_session_message] #[derive(Debug, Clone)] enum Message { IncrementPressed, DecrementPressed, TextInput(String), IcedEvent(Event), - UnLock, -} - -// You need to impl the TryInto to tell the plugin which message is the platform message -impl TryInto for Message { - type Error = Self; - fn try_into(self) -> Result { - if let Self::UnLock = self { - return Ok(UnLockAction); - } - Err(self) - } } impl MultiApplication for Counter { diff --git a/iced_sessionlock/src/lib.rs b/iced_sessionlock/src/lib.rs index aef0d35..4949e07 100644 --- a/iced_sessionlock/src/lib.rs +++ b/iced_sessionlock/src/lib.rs @@ -1,21 +1,23 @@ pub mod actions; +pub mod multi_window; +pub mod settings; + mod clipboard; mod conversion; mod error; mod event; -pub mod multi_window; mod proxy; -pub mod settings; +use iced::{Color, Element, Theme}; +use iced_futures::Subscription; +use iced_runtime::Task; -use actions::UnLockAction; -use settings::Settings; +pub use iced_sessionlock_macros::to_session_message; pub use error::Error; -use iced::{Color, Element, Theme}; -use iced_futures::Subscription; -use iced_runtime::Task; +use actions::UnLockAction; +use settings::Settings; #[derive(Debug, Clone, Copy, PartialEq)] pub struct Appearance { @@ -111,7 +113,6 @@ pub trait MultiApplication: Sized { /// Returns the current `Style` of the [`Theme`]. /// /// [`Theme`]: Self::Theme - fn style(&self, theme: &Self::Theme) -> Appearance { theme.default_style() } diff --git a/iced_sessionlock_macros/Cargo.toml b/iced_sessionlock_macros/Cargo.toml new file mode 100644 index 0000000..006dc12 --- /dev/null +++ b/iced_sessionlock_macros/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "iced_sessionlock_macros" +authors.workspace = true +edition.workspace = true +version.workspace = true +license.workspace = true +repository.workspace = true +description.workspace = true +keywords.workspace = true +readme.workspace = true + +[lib] +proc-macro = true + +[features] +default = [] +# Only nightly +diagnostics = ["darling/diagnostics"] + +[dependencies] +darling.workspace = true +manyhow.workspace = true +proc-macro2.workspace = true +quote.workspace = true +syn.workspace = true + +[dev-dependencies] +iced_sessionlock.workspace = true diff --git a/iced_sessionlock_macros/src/lib.rs b/iced_sessionlock_macros/src/lib.rs new file mode 100644 index 0000000..22cdb30 --- /dev/null +++ b/iced_sessionlock_macros/src/lib.rs @@ -0,0 +1,58 @@ +use darling::{ast::Data, util::Ignored, FromDeriveInput}; +use proc_macro2::TokenStream as TokenStream2; +use syn::{DeriveInput, Generics, Ident, Path, Variant, Visibility}; + +use quote::quote; + +#[manyhow::manyhow] +#[proc_macro_attribute] +pub fn to_session_message( + _attr: TokenStream2, + input: TokenStream2, +) -> manyhow::Result { + let derive_input = syn::parse2::(input)?; + let attrs = &derive_input.attrs; + let MessageEnum { + vis, + ident, + generics, + data, + } = MessageEnum::from_derive_input(&derive_input)?; + + let (impl_gen, ty_gen, where_gen) = generics.split_for_impl(); + let variants = data.take_enum().unwrap(); + + let unlock_action: Path = syn::parse_quote!(iced_sessionlock::actions::UnLockAction); + + let try_into = quote! { + impl #impl_gen TryInto<#unlock_action> for #ident #ty_gen #where_gen { + type Error = Self; + + fn try_into(self) -> Result<#unlock_action, Self::Error> { + match self { + Self::UnLock => Ok(#unlock_action), + _ => Err(self) + } + } + } + }; + + Ok(quote! { + #(#attrs)* + #vis enum #ident #ty_gen #where_gen { + #(#variants,)* + UnLock + } + + #try_into + }) +} + +#[derive(FromDeriveInput)] +#[darling(supports(enum_any))] +struct MessageEnum { + vis: Visibility, + ident: Ident, + generics: Generics, + data: Data, +} diff --git a/iced_sessionlock_macros/tests/macro_test.rs b/iced_sessionlock_macros/tests/macro_test.rs new file mode 100644 index 0000000..5ccc702 --- /dev/null +++ b/iced_sessionlock_macros/tests/macro_test.rs @@ -0,0 +1,13 @@ +use iced_sessionlock_macros::to_session_message; + +#[test] +fn test_macro() { + #[allow(dead_code)] + #[to_session_message] + #[derive(Debug, Clone)] + enum TestEnum { + TestA, + } + let e = TestEnum::UnLock; + let _ = e.clone(); +} From a00821b340b1281926cc5abfc3266931a9da87cc Mon Sep 17 00:00:00 2001 From: Vitalii Lukyanov Date: Sat, 28 Sep 2024 12:24:52 +0200 Subject: [PATCH 2/3] clippy lints --- iced_layershell/src/sandbox.rs | 4 ++-- iced_layershell/src/settings.rs | 2 +- layershellev/examples/simplelayer.rs | 2 +- layershellev/src/events.rs | 2 ++ sessionlockev/src/events.rs | 1 + 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/iced_layershell/src/sandbox.rs b/iced_layershell/src/sandbox.rs index 037f1ab..22a0fc1 100644 --- a/iced_layershell/src/sandbox.rs +++ b/iced_layershell/src/sandbox.rs @@ -168,13 +168,13 @@ mod tests { #[test] fn test_namespace() { let app = ::new(); - let _ = assert_eq!(LayerShellSandbox::namespace(&app), "MockSandbox"); + assert_eq!(LayerShellSandbox::namespace(&app), "MockSandbox"); } #[test] fn test_scale_factor() { let app = ::new(); - let _ = assert_eq!(LayerShellSandbox::scale_factor(&app), 2.0); + assert_eq!(LayerShellSandbox::scale_factor(&app), 2.0); } #[test] diff --git a/iced_layershell/src/settings.rs b/iced_layershell/src/settings.rs index 94cbdbf..5feeef3 100644 --- a/iced_layershell/src/settings.rs +++ b/iced_layershell/src/settings.rs @@ -109,7 +109,7 @@ mod tests { assert!(settings.fonts.is_empty()); assert_eq!(settings.default_font, Font::default()); assert_eq!(settings.default_text_size, Pixels(16.0)); - assert_eq!(settings.antialiasing, false); + assert!(!settings.antialiasing); assert!(settings.virtual_keyboard_support.is_none()); // Test default layershellv settings diff --git a/layershellev/examples/simplelayer.rs b/layershellev/examples/simplelayer.rs index 3ce009c..57fd788 100644 --- a/layershellev/examples/simplelayer.rs +++ b/layershellev/examples/simplelayer.rs @@ -41,7 +41,7 @@ fn main() { // surface outside region becomes transparent for input events // To ignore all input events use region with (0,0) size for x in ev.get_unit_iter() { - let region = compositor.create_region(&qh, ()); + let region = compositor.create_region(qh, ()); region.add(0, 0, 0, 0); x.get_wlsurface().set_input_region(Some(®ion)); } diff --git a/layershellev/src/events.rs b/layershellev/src/events.rs index ab4c469..b8df1fa 100644 --- a/layershellev/src/events.rs +++ b/layershellev/src/events.rs @@ -108,6 +108,8 @@ impl Default for NewLayerShellSettings { } } } + +#[allow(clippy::too_long_first_doc_paragraph)] /// the return data /// Note: when event is RequestBuffer, you must return WlBuffer /// Note: when receive InitRequest, you can request to bind extra wayland-protocols. this time you diff --git a/sessionlockev/src/events.rs b/sessionlockev/src/events.rs index 083f9c1..3b3abcb 100644 --- a/sessionlockev/src/events.rs +++ b/sessionlockev/src/events.rs @@ -55,6 +55,7 @@ pub enum SessionLockEvent<'a, T, Message> { UserEvent(Message), } +#[allow(clippy::too_long_first_doc_paragraph)] /// the return data /// Note: when event is RequestBuffer, you must return WlBuffer /// Note: when receive InitRequest, you can request to bind extra wayland-protocols. this time you From ff8b52b5c2a126c97b662afec6ce9dba5433dc5b Mon Sep 17 00:00:00 2001 From: Vitalii Lukyanov Date: Sat, 28 Sep 2024 12:44:58 +0200 Subject: [PATCH 3/3] unknown lints on stable, so removing --- layershellev/src/events.rs | 1 - sessionlockev/src/events.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/layershellev/src/events.rs b/layershellev/src/events.rs index b8df1fa..cef3b76 100644 --- a/layershellev/src/events.rs +++ b/layershellev/src/events.rs @@ -109,7 +109,6 @@ impl Default for NewLayerShellSettings { } } -#[allow(clippy::too_long_first_doc_paragraph)] /// the return data /// Note: when event is RequestBuffer, you must return WlBuffer /// Note: when receive InitRequest, you can request to bind extra wayland-protocols. this time you diff --git a/sessionlockev/src/events.rs b/sessionlockev/src/events.rs index 3b3abcb..083f9c1 100644 --- a/sessionlockev/src/events.rs +++ b/sessionlockev/src/events.rs @@ -55,7 +55,6 @@ pub enum SessionLockEvent<'a, T, Message> { UserEvent(Message), } -#[allow(clippy::too_long_first_doc_paragraph)] /// the return data /// Note: when event is RequestBuffer, you must return WlBuffer /// Note: when receive InitRequest, you can request to bind extra wayland-protocols. this time you