Skip to content

Commit

Permalink
feat: support record mainwindow (#101)
Browse files Browse the repository at this point in the history
* feat: support record mainwindow

* feat: change the macro name

* fix: unit test

* fix: remove shell maybe cause panic

* chore: add some documents

* fix(doc): the document cannot be rendered
  • Loading branch information
Decodetalkers authored Dec 6, 2024
1 parent 8e5bc54 commit ec7a085
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 32 deletions.
10 changes: 8 additions & 2 deletions iced_examples/counter_mulit_pattern/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced_runtime::{task, Action};
use iced_layershell::build_pattern::{daemon, MainSettings};
use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::settings::{LayerShellSettings, StartMode};
use iced_layershell::{to_layer_message, LayerSingleton};
use iced_layershell::{to_layer_message, WindowInfoMarker};

pub fn main() -> Result<(), iced_layershell::Error> {
tracing_subscriber::fmt().init();
Expand Down Expand Up @@ -43,13 +43,15 @@ struct Counter {
ids: HashMap<iced::window::Id, WindowInfo>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, LayerSingleton)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, WindowInfoMarker)]
enum WindowInfo {
#[singleton]
Left,
#[singleton]
Right,
PopUp,
#[main]
Main,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -101,6 +103,10 @@ impl Counter {
}

fn set_id_info(&mut self, id: iced::window::Id, info: WindowInfo) {
if let WindowInfo::Main = info {
println!("it is main window: {id}");
return;
}
self.ids.insert(id, info);
}

Expand Down
4 changes: 2 additions & 2 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced_runtime::{task, Action};
use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::MultiApplication;
use iced_layershell::{to_layer_message, LayerSingleton};
use iced_layershell::{to_layer_message, WindowInfoMarker};

pub fn main() -> Result<(), iced_layershell::Error> {
tracing_subscriber::fmt().init();
Expand All @@ -32,7 +32,7 @@ struct Counter {
ids: HashMap<iced::window::Id, WindowInfo>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, LayerSingleton)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, WindowInfoMarker)]
enum WindowInfo {
#[singleton]
Left,
Expand Down
9 changes: 9 additions & 0 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ pub trait IsSingleton {
}
}

pub struct MainWindowInfo;

impl TryFrom<MainWindowInfo> for () {
type Error = ();
fn try_from(_: MainWindowInfo) -> Result<(), Self::Error> {
Err(())
}
}

impl IsSingleton for () {}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down
8 changes: 8 additions & 0 deletions iced_layershell/src/build_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! The build_pattern allow you to create application just with callback functions.
//! Similar with the one of origin iced.
mod application;
mod daemon;
use std::borrow::Cow;
Expand All @@ -11,6 +14,8 @@ pub trait Renderer: iced_core::text::Renderer + iced_graphics::compositor::Defau

impl<T> Renderer for T where T: iced_core::text::Renderer + iced_graphics::compositor::Default {}

/// MainSettings for iced_layershell
/// different from [`crate::Settings`], it does not contain the field of flags
#[derive(Debug)]
pub struct MainSettings {
/// The identifier of the application.
Expand Down Expand Up @@ -62,5 +67,8 @@ impl Default for MainSettings {
}
}

#[doc = include_str!("./build_pattern/application.md")]
pub use application::application;

#[doc = include_str!("./build_pattern/daemon.md")]
pub use daemon::daemon;
160 changes: 160 additions & 0 deletions iced_layershell/src/build_pattern/application.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# The application function allow you to create the application with just some functions

```rust, no_run
use iced::widget::{button, column, row, text, text_input};
use iced::{event, Alignment, Color, Element, Event, Length, Task as Command};
use iced_layershell::build_pattern::{application, MainSettings};
use iced_layershell::reexport::Anchor;
use iced_layershell::settings::{LayerShellSettings, StartMode};
use iced_layershell::to_layer_message;
pub fn main() -> Result<(), iced_layershell::Error> {
let args: Vec<String> = std::env::args().collect();
let mut binded_output_name = None;
if args.len() >= 2 {
binded_output_name = Some(args[1].to_string())
}
let start_mode = match binded_output_name {
Some(output) => StartMode::TargetScreen(output),
None => StartMode::Active,
};
application(namespace, update, view)
.style(style)
.subscription(subscription)
.settings(MainSettings {
layer_settings: LayerShellSettings {
size: Some((0, 400)),
exclusive_zone: 400,
anchor: Anchor::Bottom | Anchor::Left | Anchor::Right,
start_mode,
..Default::default()
},
..Default::default()
})
.run()
}
#[derive(Default)]
struct Counter {
value: i32,
text: String,
}
#[derive(Debug, Clone, Copy)]
enum WindowDirection {
Top,
Left,
Right,
Bottom,
}
#[to_layer_message]
#[derive(Debug, Clone)]
enum Message {
IncrementPressed,
DecrementPressed,
TextInput(String),
Direction(WindowDirection),
IcedEvent(Event),
}
fn namespace(_: &Counter) -> String {
String::from("Counter - Iced")
}
fn subscription(_: &Counter) -> iced::Subscription<Message> {
event::listen().map(Message::IcedEvent)
}
fn update(counter: &mut Counter, message: Message) -> Command<Message> {
match message {
Message::IcedEvent(event) => {
println!("hello {event:?}");
Command::none()
}
Message::IncrementPressed => {
counter.value += 1;
Command::none()
}
Message::DecrementPressed => {
counter.value -= 1;
Command::none()
}
Message::TextInput(text) => {
counter.text = text;
Command::none()
}
Message::Direction(direction) => match direction {
WindowDirection::Left => Command::done(Message::AnchorSizeChange(
Anchor::Left | Anchor::Top | Anchor::Bottom,
(400, 0),
)),
WindowDirection::Right => Command::done(Message::AnchorSizeChange(
Anchor::Right | Anchor::Top | Anchor::Bottom,
(400, 0),
)),
WindowDirection::Bottom => Command::done(Message::AnchorSizeChange(
Anchor::Bottom | Anchor::Left | Anchor::Right,
(0, 400),
)),
WindowDirection::Top => Command::done(Message::AnchorSizeChange(
Anchor::Top | Anchor::Left | Anchor::Right,
(0, 400),
)),
},
_ => unreachable!(),
}
}
fn view(counter: &Counter) -> Element<Message> {
let center = column![
button("Increment").on_press(Message::IncrementPressed),
text(counter.value).size(50),
button("Decrement").on_press(Message::DecrementPressed)
]
.align_x(Alignment::Center)
.padding(20)
.width(Length::Fill)
.height(Length::Fill);
row![
button("left")
.on_press(Message::Direction(WindowDirection::Left))
.height(Length::Fill),
column![
button("top")
.on_press(Message::Direction(WindowDirection::Top))
.width(Length::Fill),
center,
text_input("hello", &counter.text)
.on_input(Message::TextInput)
.padding(10),
button("bottom")
.on_press(Message::Direction(WindowDirection::Bottom))
.width(Length::Fill),
]
.width(Length::Fill),
button("right")
.on_press(Message::Direction(WindowDirection::Right))
.height(Length::Fill),
]
.padding(20)
.spacing(10)
.width(Length::Fill)
.height(Length::Fill)
.into()
}
fn style(_counter: &Counter, theme: &iced::Theme) -> iced_layershell::Appearance {
use iced_layershell::Appearance;
Appearance {
background_color: Color::TRANSPARENT,
text_color: theme.palette().text,
}
}
```
Loading

0 comments on commit ec7a085

Please sign in to comment.