Skip to content

Commit

Permalink
feat: prepare for open new window (#34)
Browse files Browse the repository at this point in the history
* feat: prepair for open new window

* chore: pass in info

* chore: change the name of `F`

* chore: do fmt

* feat: base example

* feat: remove buffer

* chore: mainly finished

* chore: add some comments

* chore: run format

* fix: panic

* chore: cargo clippy

* chore: do fmt

* chore: tidy up

* chore: do fmt

* chore: add more option

* chore: little code style adjust

* refactor: remove INFO

* refactor: remove need for Debug

* chore: tidy up code

* fix: some action do not work

* chore: code tidyup
  • Loading branch information
Decodetalkers authored Aug 6, 2024
1 parent bd8c467 commit da73950
Show file tree
Hide file tree
Showing 14 changed files with 616 additions and 190 deletions.
126 changes: 111 additions & 15 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
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};
use iced_layershell::reexport::Anchor;
use iced_layershell::actions::{
LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithIdAndInfo,
LayershellCustomActionsWithInfo,
};
use iced_runtime::command::Action;
use iced_runtime::window::Action as WindowAction;

use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::settings::{LayerShellSettings, Settings};
use iced_layershell::MultiApplication;
pub fn main() -> Result<(), iced_layershell::Error> {
Expand All @@ -20,6 +28,13 @@ pub fn main() -> Result<(), iced_layershell::Error> {
struct Counter {
value: i32,
text: String,
ids: HashMap<iced::window::Id, WindowInfo>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WindowInfo {
Left,
Right,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -34,27 +49,55 @@ enum WindowDirection {
enum Message {
IncrementPressed,
DecrementPressed,
NewWindowLeft,
NewWindowRight,
Close(Id),
TextInput(String),
Direction(WindowDirection),
IcedEvent(Event),
}

impl Counter {
fn window_id(&self, info: &WindowInfo) -> Option<&iced::window::Id> {
for (k, v) in self.ids.iter() {
if info == v {
return Some(k);
}
}
None
}
}

impl MultiApplication for Counter {
type Message = Message;
type Flags = ();
type Theme = Theme;
type Executor = iced::executor::Default;
type WindowInfo = WindowInfo;

fn new(_flags: ()) -> (Self, Command<Message>) {
(
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 remove_id(&mut self, id: iced::window::Id) {
self.ids.remove(&id);
}

fn namespace(&self) -> String {
String::from("Counter - Iced")
}
Expand All @@ -64,9 +107,20 @@ impl MultiApplication for Counter {
}

fn update(&mut self, message: Message) -> Command<Message> {
use iced::keyboard;
use iced::keyboard::key::Named;
use iced::Event;
match message {
Message::IcedEvent(event) => {
println!("hello {event:?}");
if let Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Named(Named::Escape),
..
}) = event
{
if let Some(id) = self.window_id(&WindowInfo::Left) {
return Command::single(Action::Window(WindowAction::Close(*id)));
}
}
Command::none()
}
Message::IncrementPressed => {
Expand All @@ -84,7 +138,7 @@ impl MultiApplication for Counter {
Message::Direction(direction) => match direction {
WindowDirection::Left(id) => Command::batch(vec![
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::AnchorChange(
Anchor::Left | Anchor::Top | Anchor::Bottom,
Expand All @@ -93,7 +147,7 @@ impl MultiApplication for Counter {
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((400, 0)),
)
Expand All @@ -102,7 +156,7 @@ impl MultiApplication for Counter {
]),
WindowDirection::Right(id) => Command::batch(vec![
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::AnchorChange(
Anchor::Right | Anchor::Top | Anchor::Bottom,
Expand All @@ -111,7 +165,7 @@ impl MultiApplication for Counter {
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((400, 0)),
)
Expand All @@ -120,7 +174,7 @@ impl MultiApplication for Counter {
]),
WindowDirection::Bottom(id) => Command::batch(vec![
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::AnchorChange(
Anchor::Bottom | Anchor::Left | Anchor::Right,
Expand All @@ -129,41 +183,83 @@ impl MultiApplication for Counter {
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((0, 400)),
LayershellCustomActionsWithInfo::SizeChange((0, 400)),
)
.into(),
),
]),
WindowDirection::Top(id) => Command::batch(vec![
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::AnchorChange(
LayershellCustomActionsWithInfo::AnchorChange(
Anchor::Top | Anchor::Left | Anchor::Right,
),
)
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((0, 400)),
)
.into(),
),
]),
},
Message::NewWindowLeft => Command::single(
LayershellCustomActionsWithIdAndInfo::new(
iced::window::Id::MAIN,
LayershellCustomActionsWithInfo::NewLayerShell((
NewLayerShellSettings {
size: Some((100, 100)),
exclusize_zone: None,
anchor: Anchor::Left | Anchor::Bottom,
layer: Layer::Top,
margins: None,
keyboard_interactivity: KeyboardInteractivity::Exclusive,
},
WindowInfo::Left,
)),
)
.into(),
),
Message::NewWindowRight => Command::single(
LayershellCustomActionsWithIdAndInfo::new(
iced::window::Id::MAIN,
LayershellCustomActionsWithInfo::NewLayerShell((
NewLayerShellSettings {
size: Some((100, 100)),
exclusize_zone: None,
anchor: Anchor::Right | Anchor::Bottom,
layer: Layer::Top,
margins: None,
keyboard_interactivity: KeyboardInteractivity::None,
},
WindowInfo::Right,
)),
)
.into(),
),
Message::Close(id) => Command::single(Action::Window(WindowAction::Close(id))),
}
}

fn view(&self, id: iced::window::Id) -> Element<Message> {
//println!("{:?}, {}", _id, self.value);
if let Some(WindowInfo::Left) = self.id_info(id) {
return button("close left").on_press(Message::Close(id)).into();
}
if let Some(WindowInfo::Right) = self.id_info(id) {
return button("close right").on_press(Message::Close(id)).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)
Expand Down
56 changes: 41 additions & 15 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,68 @@ use iced::window::Id as IcedId;
use iced_core::mouse::Interaction;
use iced_runtime::command::Action;
use layershellev::id::Id as LayerId;
use layershellev::NewLayerShellSettings;
#[allow(unused)]
#[derive(Debug, Clone)]
pub(crate) enum LayerShellActions {
pub(crate) enum LayerShellActions<INFO: Clone> {
Mouse(Interaction),
CustomActions(Vec<LayershellCustomActions>),
CustomActionsWithId(Vec<LayershellCustomActionsWithIdInner>),
CustomActions(Vec<LayershellCustomActionsWithInfo<INFO>>),
CustomActionsWithId(Vec<LayershellCustomActionsWithIdInner<INFO>>),
RedrawAll,
RedrawWindow(LayerId),
}

#[derive(Debug, Clone, Copy)]
pub enum LayershellCustomActions {
pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
AnchorChange(Anchor),
LayerChange(Layer),
SizeChange((u32, u32)),
VirtualKeyboardPressed { time: u32, key: u32 },
VirtualKeyboardPressed {
time: u32,
key: u32,
},
NewLayerShell((NewLayerShellSettings, INFO)),
/// is same with WindowAction::Close(id)
RemoveLayerShell(IcedId),
}

pub type LayershellCustomActions = LayershellCustomActionsWithInfo<()>;

#[derive(Debug, Clone, Copy)]
pub struct LayershellCustomActionsWithId(pub IcedId, pub LayershellCustomActions);
pub struct LayershellCustomActionsWithIdAndInfo<INFO: Clone>(
pub IcedId,
pub LayershellCustomActionsWithInfo<INFO>,
);

impl<INFO: Clone> LayershellCustomActionsWithIdAndInfo<INFO> {
pub fn new(id: IcedId, actions: LayershellCustomActionsWithInfo<INFO>) -> Self {
Self(id, actions)
}
}

pub type LayershellCustomActionsWithId = LayershellCustomActionsWithIdAndInfo<()>;

// first one means
#[derive(Debug, Clone, Copy)]
pub(crate) struct LayershellCustomActionsWithIdInner(pub LayerId, pub LayershellCustomActions);
pub(crate) struct LayershellCustomActionsWithIdInner<INFO: Clone>(
pub LayerId, // come from
pub Option<LayerId>, // target if has one
pub LayershellCustomActionsWithInfo<INFO>, // actions
);

impl<T> From<LayershellCustomActionsWithId> for Action<T> {
fn from(value: LayershellCustomActionsWithId) -> Self {
Action::Custom(Box::new(value))
impl<T, INFO: Clone + 'static> From<LayershellCustomActionsWithIdAndInfo<INFO>> for Action<T> {
fn from(value: LayershellCustomActionsWithIdAndInfo<INFO>) -> Self {
Action::Custom(Box::new(value.clone()))
}
}
impl<T> From<LayershellCustomActions> for Action<T> {
fn from(value: LayershellCustomActions) -> Self {
Action::Custom(Box::new(value))
impl<T, INFO: Clone + 'static> From<LayershellCustomActionsWithInfo<INFO>> for Action<T> {
fn from(value: LayershellCustomActionsWithInfo<INFO>) -> Self {
Action::Custom(Box::new(value.clone()))
}
}

impl LayershellCustomActions {
impl<INFO: Clone + 'static> LayershellCustomActionsWithInfo<INFO> {
pub fn to_action<T>(&self) -> Action<T> {
(*self).into()
(*self).clone().into()
}
}
Loading

0 comments on commit da73950

Please sign in to comment.