Skip to content

Commit

Permalink
feat: add build_pattern for iced_layershell
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Dec 3, 2024
1 parent a08d676 commit 62c5421
Show file tree
Hide file tree
Showing 7 changed files with 2,040 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions iced_examples/counter_pattern/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "counter_pattern"
authors.workspace = true
edition.workspace = true
version.workspace = true
license.workspace = true
repository.workspace = true
description.workspace = true
keywords.workspace = true
readme.workspace = true

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

[dependencies]
iced.workspace = true
iced_runtime.workspace = true
iced_layershell.workspace = true
154 changes: 154 additions & 0 deletions iced_examples/counter_pattern/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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,
}
}
66 changes: 66 additions & 0 deletions iced_layershell/src/build_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
mod application;
mod daemon;
use std::borrow::Cow;

use iced::{Font, Pixels};

use crate::settings::{LayerShellSettings, VirtualKeyboardSettings};

/// The renderer of some [`Program`].
pub trait Renderer: iced_core::text::Renderer + iced_graphics::compositor::Default {}

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

#[derive(Debug)]
pub struct MainSettings {
/// The identifier of the application.
///
/// If provided, this identifier may be used to identify the application or
/// communicate with it through the windowing system.
pub id: Option<String>,

/// settings for layer shell
pub layer_settings: LayerShellSettings,
/// The data needed to initialize an [`Application`].
///
/// The fonts to load on boot.
pub fonts: Vec<Cow<'static, [u8]>>,

/// The default [`Font`] to be used.
///
/// By default, it uses [`Family::SansSerif`](iced::font::Family::SansSerif).
pub default_font: Font,

/// The text size that will be used by default.
///
/// The default value is `16.0`.
pub default_text_size: Pixels,

/// If set to true, the renderer will try to perform antialiasing for some
/// primitives.
///
/// Enabling it can produce a smoother result in some widgets, like the
/// `Canvas`, at a performance cost.
///
/// By default, it is disabled.
///
pub antialiasing: bool,

pub virtual_keyboard_support: Option<VirtualKeyboardSettings>,
}
impl Default for MainSettings {
fn default() -> Self {
MainSettings {
id: None,
fonts: Vec::new(),
layer_settings: LayerShellSettings::default(),
default_font: Font::default(),
default_text_size: Pixels(16.0),
antialiasing: false,
virtual_keyboard_support: None,
}
}
}

pub use application::application;
pub use daemon::daemon;
Loading

0 comments on commit 62c5421

Please sign in to comment.