diff --git a/iced_examples/counter_muti/src/main.rs b/iced_examples/counter_muti/src/main.rs index 5e5f76d..8cc98ee 100644 --- a/iced_examples/counter_muti/src/main.rs +++ b/iced_examples/counter_muti/src/main.rs @@ -1,10 +1,13 @@ +use std::collections::HashMap; + use iced::widget::{button, column, row, text, text_input}; use iced::window::Id; use iced::{event, Alignment, Command, Element, Event, Length, Theme}; use iced_layershell::actions::{ - LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithInfo, + LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithIdAndInfo, + LayershellCustomActionsWithInfo, }; -use iced_layershell::reexport::Anchor; +use iced_layershell::reexport::{Anchor, Layer, NewLayerShellSettings}; use iced_layershell::settings::{LayerShellSettings, Settings}; use iced_layershell::MultiApplication; pub fn main() -> Result<(), iced_layershell::Error> { @@ -22,6 +25,13 @@ pub fn main() -> Result<(), iced_layershell::Error> { struct Counter { value: i32, text: String, + ids: HashMap, +} + +#[derive(Debug, Clone, Copy)] +enum WindowInfo { + Left, + Right, } #[derive(Debug, Clone, Copy)] @@ -36,6 +46,8 @@ enum WindowDirection { enum Message { IncrementPressed, DecrementPressed, + NewWindowLeft, + NewWindowRight, TextInput(String), Direction(WindowDirection), IcedEvent(Event), @@ -46,18 +58,27 @@ impl MultiApplication for Counter { type Flags = (); type Theme = Theme; type Executor = iced::executor::Default; - type WindowInfo = (); + type WindowInfo = WindowInfo; fn new(_flags: ()) -> (Self, Command) { ( Self { value: 0, text: "eee".to_string(), + ids: HashMap::new(), }, Command::none(), ) } + fn id_info(&self, id: iced::window::Id) -> Option<&Self::WindowInfo> { + self.ids.get(&id) + } + + fn set_id_info(&mut self, id: iced::window::Id, info: Self::WindowInfo) { + self.ids.insert(id, info); + } + fn namespace(&self) -> String { String::from("Counter - Iced") } @@ -158,14 +179,54 @@ impl MultiApplication for Counter { ), ]), }, + Message::NewWindowLeft => Command::single( + LayershellCustomActionsWithIdAndInfo::new( + iced::window::Id::MAIN, + LayershellCustomActionsWithInfo::NewLayerShell(( + NewLayerShellSettings { + size: Some((100, 100)), + exclude_zone: None, + anchor: Anchor::Left | Anchor::Bottom, + layer: Layer::Top, + margin: None, + }, + WindowInfo::Left, + )), + ) + .into(), + ), + Message::NewWindowRight => Command::single( + LayershellCustomActionsWithIdAndInfo::new( + iced::window::Id::MAIN, + LayershellCustomActionsWithInfo::NewLayerShell(( + NewLayerShellSettings { + size: Some((100, 100)), + exclude_zone: None, + anchor: Anchor::Right | Anchor::Bottom, + layer: Layer::Top, + margin: None, + }, + WindowInfo::Right, + )), + ) + .into(), + ), } } fn view(&self, id: iced::window::Id) -> Element { + if let Some(WindowInfo::Left) = self.id_info(id) { + return text("left").into(); + } + if let Some(WindowInfo::Right) = self.id_info(id) { + return text("right").into(); + } let center = column![ button("Increment").on_press(Message::IncrementPressed), text(self.value).size(50), - button("Decrement").on_press(Message::DecrementPressed) + button("Decrement").on_press(Message::DecrementPressed), + button("newwindowLeft").on_press(Message::NewWindowLeft), + button("newwindowRight").on_press(Message::NewWindowRight) ] .padding(20) .align_items(Alignment::Center) diff --git a/iced_layershell/src/lib.rs b/iced_layershell/src/lib.rs index 2407157..8ef68b8 100644 --- a/iced_layershell/src/lib.rs +++ b/iced_layershell/src/lib.rs @@ -241,7 +241,7 @@ pub trait MultiApplication: Sized { /// title of your window when necessary. fn namespace(&self) -> String; - fn id_info(&self, _id: iced_core::window::Id) -> Option { + fn id_info(&self, _id: iced_core::window::Id) -> Option<&Self::WindowInfo> { None } @@ -393,7 +393,7 @@ where self.0.scale_factor(window) } - fn id_info(&self, id: iced_core::window::Id) -> Option { + fn id_info(&self, id: iced_core::window::Id) -> Option<&Self::WindowInfo> { self.0.id_info(id) } diff --git a/iced_layershell/src/multi_window.rs b/iced_layershell/src/multi_window.rs index df3a9e2..3624807 100644 --- a/iced_layershell/src/multi_window.rs +++ b/iced_layershell/src/multi_window.rs @@ -78,7 +78,8 @@ where self.namespace() } - fn id_info(&self, _id: iced_core::window::Id) -> Option; + fn id_info(&self, _id: iced_core::window::Id) -> Option<&Self::WindowInfo>; + fn set_id_info(&mut self, _id: iced_core::window::Id, info: Self::WindowInfo); /// Returns the current [`Theme`] of the [`Application`]. diff --git a/layershellev/src/lib.rs b/layershellev/src/lib.rs index deaeac2..b713a92 100644 --- a/layershellev/src/lib.rs +++ b/layershellev/src/lib.rs @@ -1819,7 +1819,7 @@ impl WindowState { zxdgoutput: None, fractional_scale, binding: None, - becreated: false, + becreated: true, info, }); }