Skip to content

Commit

Permalink
feat: add startmode to layershellev (#74)
Browse files Browse the repository at this point in the history
* feat: add startmode to layershellev

use enum to split the mode

* chore: add init finished check

* fix: only monitor allscreen do newdisplay event

* fix: test

* chore: adjust the settings of iced_layershell

* chore: adjust background logic

* chore: adjust the field name of StartMode

* fix: examples

* chore: adjust function name
  • Loading branch information
Decodetalkers authored Oct 6, 2024
1 parent be5d628 commit 1e37743
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 51 deletions.
9 changes: 7 additions & 2 deletions iced_examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced::widget::{button, column, row, text, text_input};
use iced::{event, Alignment, Color, Element, Event, Length, Task as Command, Theme};
use iced_layershell::reexport::Anchor;
use iced_layershell::settings::{LayerShellSettings, Settings};
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::to_layer_message;
use iced_layershell::Application;

Expand All @@ -13,12 +13,17 @@ pub fn main() -> Result<(), iced_layershell::Error> {
binded_output_name = Some(args[1].to_string())
}

let start_mode = match binded_output_name {
Some(output) => StartMode::TargetScreen(output),
None => StartMode::Active,
};

Counter::run(Settings {
layer_settings: LayerShellSettings {
size: Some((0, 400)),
exclusive_zone: 400,
anchor: Anchor::Bottom | Anchor::Left | Anchor::Right,
binded_output_name,
start_mode,
..Default::default()
},
..Default::default()
Expand Down
4 changes: 3 additions & 1 deletion iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use iced_runtime::window::Action as WindowAction;
use iced_runtime::{task, Action};

use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::settings::{LayerShellSettings, Settings};
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::to_layer_message;
use iced_layershell::MultiApplication;

pub fn main() -> Result<(), iced_layershell::Error> {
Counter::run(Settings {
layer_settings: LayerShellSettings {
size: Some((0, 400)),
exclusive_zone: 400,
anchor: Anchor::Bottom | Anchor::Left | Anchor::Right,
start_mode: StartMode::AllScreens,
..Default::default()
},
..Default::default()
Expand Down
12 changes: 8 additions & 4 deletions iced_examples/zbus_invoked_widget/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iced_runtime::window::Action as WindowAction;
use iced_runtime::Action;

use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::settings::Settings;
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::MultiApplication;
use zbus::{interface, ConnectionBuilder};

Expand All @@ -23,7 +23,13 @@ struct Counter {
text: String,
}
pub fn main() -> Result<(), iced_layershell::Error> {
Counter::run(Settings::default())
Counter::run(Settings {
layer_settings: LayerShellSettings {
start_mode: StartMode::Background,
..Default::default()
},
..Default::default()
})
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -64,8 +70,6 @@ impl MultiApplication for Counter {
type Executor = iced::executor::Default;
type WindowInfo = ();

const BACKGROUND_MODE: bool = true;

fn set_id_info(&mut self, _id: iced_runtime::core::window::Id, _info: Self::WindowInfo) {
self.window_shown = true;
}
Expand Down
10 changes: 7 additions & 3 deletions iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use iced_futures::{Executor, Runtime, Subscription};
use layershellev::{
calloop::timer::{TimeoutAction, Timer},
reexport::zwp_virtual_keyboard_v1,
LayerEvent, ReturnData, WindowWrapper,
LayerEvent, ReturnData, StartMode, WindowWrapper,
};

use futures::{channel::mpsc, StreamExt};
Expand Down Expand Up @@ -149,16 +149,20 @@ where
runtime.enter(|| A::new(flags))
};

assert!(!matches!(
settings.layer_settings.start_mode,
StartMode::AllScreens | StartMode::Background
));

let ev = layershellev::WindowStateSimple::new(&application.namespace())
.with_single(true)
.with_use_display_handle(true)
.with_option_size(settings.layer_settings.size)
.with_layer(settings.layer_settings.layer)
.with_anchor(settings.layer_settings.anchor)
.with_exclusize_zone(settings.layer_settings.exclusive_zone)
.with_margin(settings.layer_settings.margin)
.with_keyboard_interacivity(settings.layer_settings.keyboard_interactivity)
.with_xdg_output_name(settings.layer_settings.binded_output_name)
.with_start_mode(settings.layer_settings.start_mode)
.build()
.expect("Cannot create layershell");

Expand Down
4 changes: 0 additions & 4 deletions iced_layershell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ pub trait MultiApplication: Sized {

type Theme: Default + DefaultStyle;

const BACKGROUND_MODE: bool = false;

/// Initializes the [`Application`] with the flags provided to
/// [`run`] as part of the [`Settings`].
///
Expand Down Expand Up @@ -409,8 +407,6 @@ where

type WindowInfo = A::WindowInfo;

const BACKGROUND_MODE: bool = A::BACKGROUND_MODE;

fn new(flags: Self::Flags) -> (Self, Task<A::Message>) {
let (app, command) = A::new(flags);

Expand Down
12 changes: 6 additions & 6 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ where

type WindowInfo;

const BACKGROUND_MODE: bool;

/// Initializes the [`Application`] with the flags provided to
/// [`run`] as part of the [`Settings`].
///
Expand Down Expand Up @@ -187,9 +185,9 @@ where
runtime.enter(|| application.subscription().map(Action::Output)),
));

let is_background_mode = settings.layer_settings.start_mode.is_background();
let ev: WindowState<A::WindowInfo> = layershellev::WindowState::new(&application.namespace())
.with_single(false)
.with_background(A::BACKGROUND_MODE)
.with_start_mode(settings.layer_settings.start_mode)
.with_use_display_handle(true)
.with_option_size(settings.layer_settings.size)
.with_layer(settings.layer_settings.layer)
Expand All @@ -215,6 +213,7 @@ where
event_receiver,
control_sender,
window,
is_background_mode,
));

let mut context = task::Context::from_waker(task::noop_waker_ref());
Expand Down Expand Up @@ -461,6 +460,7 @@ async fn run_instance<A, E, C>(
>,
mut control_sender: mpsc::UnboundedSender<LayerShellActions<A::WindowInfo>>,
window: Arc<WindowWrapper>,
is_background_mode: bool,
) where
A: Application + 'static,
E: Executor + 'static,
Expand Down Expand Up @@ -746,7 +746,7 @@ async fn run_instance<A, E, C>(
// HACK: this logic is just from iced, but seems if there is no main window,
// any window will not get Outdated state.
// So here just check if there is window_events
if A::BACKGROUND_MODE && has_window_event {
if is_background_mode && has_window_event {
custom_actions.push(LayerShellActions::RedrawAll);
}

Expand All @@ -765,7 +765,7 @@ async fn run_instance<A, E, C>(
window.state.synchronize(&application);
}

if !A::BACKGROUND_MODE {
if !is_background_mode {
custom_actions.push(LayerShellActions::RedrawAll);
}

Expand Down
17 changes: 11 additions & 6 deletions iced_layershell/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use iced::{Font, Pixels};

use crate::reexport::{Anchor, KeyboardInteractivity, Layer};

pub use layershellev::StartMode;

use layershellev::reexport::wayland_client::wl_keyboard::KeymapFormat;

#[derive(Debug)]
Expand Down Expand Up @@ -80,7 +82,7 @@ pub struct LayerShellSettings {
pub size: Option<(u32, u32)>,
pub margin: (i32, i32, i32, i32),
pub keyboard_interactivity: KeyboardInteractivity,
pub binded_output_name: Option<String>,
pub start_mode: StartMode,
}

impl Default for LayerShellSettings {
Expand All @@ -92,7 +94,7 @@ impl Default for LayerShellSettings {
size: None,
margin: (0, 0, 0, 0),
keyboard_interactivity: KeyboardInteractivity::OnDemand,
binded_output_name: None,
start_mode: StartMode::default(),
}
}
}
Expand Down Expand Up @@ -125,7 +127,10 @@ mod tests {
settings.layer_settings.keyboard_interactivity,
KeyboardInteractivity::OnDemand
);
assert!(settings.layer_settings.binded_output_name.is_none());
assert!(matches!(
settings.layer_settings.start_mode,
StartMode::Active
));
}

#[test]
Expand Down Expand Up @@ -153,7 +158,7 @@ mod tests {
size: Some((1920, 1080)),
margin: (10, 10, 10, 10),
keyboard_interactivity: KeyboardInteractivity::None,
binded_output_name: Some("HDMI-1".to_string()),
start_mode: StartMode::TargetScreen("HDMI-1".to_string()),
};

assert_eq!(layer_settings.anchor, Anchor::Top | Anchor::Left);
Expand All @@ -166,8 +171,8 @@ mod tests {
KeyboardInteractivity::None
);
assert_eq!(
layer_settings.binded_output_name,
Some("HDMI-1".to_string())
layer_settings.start_mode,
StartMode::TargetScreen("HDMI-1".to_string())
);
}
}
2 changes: 1 addition & 1 deletion layershellev/examples/simplelayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use layershellev::*;

fn main() {
let ev: WindowState<()> = WindowState::new("Hello")
.with_single(false)
.with_allscreens()
.with_size((0, 400))
.with_layer(Layer::Top)
.with_margin((20, 20, 100, 20))
Expand Down
Loading

0 comments on commit 1e37743

Please sign in to comment.