Skip to content

Commit

Permalink
chore: pass in info
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Aug 5, 2024
1 parent a9daa28 commit dda9c44
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 151 deletions.
25 changes: 13 additions & 12 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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::actions::{
LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithInfo,
};
use iced_layershell::reexport::Anchor;
use iced_layershell::settings::{LayerShellSettings, Settings};
use iced_layershell::MultiApplication;
Expand Down Expand Up @@ -85,7 +87,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 @@ -94,7 +96,7 @@ impl MultiApplication for Counter {
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((400, 0)),
)
Expand All @@ -103,7 +105,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 @@ -112,7 +114,7 @@ impl MultiApplication for Counter {
.into(),
),
Command::single(
LayershellCustomActionsWithId(
LayershellCustomActionsWithId::new(
id,
LayershellCustomActions::SizeChange((400, 0)),
)
Expand All @@ -121,7 +123,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 @@ -130,25 +132,25 @@ 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)),
)
Expand All @@ -160,7 +162,6 @@ impl MultiApplication for Counter {
}

fn view(&self, id: iced::window::Id) -> Element<Message> {
//println!("{:?}, {}", _id, self.value);
let center = column![
button("Increment").on_press(Message::IncrementPressed),
text(self.value).size(50),
Expand Down
50 changes: 36 additions & 14 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,64 @@ 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 },
NewLayerShell((NewLayerShellSettings, INFO)),
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,
pub Option<LayerId>,
pub LayershellCustomActionsWithInfo<INFO>,
);

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()
}
}
27 changes: 14 additions & 13 deletions iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod state;
use std::{mem::ManuallyDrop, os::fd::AsFd, sync::Arc, time::Duration};

use crate::{
actions::{LayerShellActions, LayershellCustomActions},
actions::{LayerShellActions, LayershellCustomActionsWithInfo},
clipboard::LayerShellClipboard,
conversion,
error::Error,
Expand Down Expand Up @@ -147,7 +147,7 @@ where
runtime.enter(|| A::new(flags))
};

let ev: WindowState<()> = layershellev::WindowState::new(&application.namespace())
let ev: WindowState<(), ()> = layershellev::WindowState::new(&application.namespace())
.with_single(true)
.with_use_display_handle(true)
.with_option_size(settings.layer_settings.size)
Expand All @@ -171,8 +171,8 @@ where

let state = State::new(&application, &ev);

let (mut event_sender, event_receiver) = mpsc::unbounded::<IcedLayerEvent<A::Message>>();
let (control_sender, mut control_receiver) = mpsc::unbounded::<Vec<LayerShellActions>>();
let (mut event_sender, event_receiver) = mpsc::unbounded::<IcedLayerEvent<A::Message, ()>>();
let (control_sender, mut control_receiver) = mpsc::unbounded::<Vec<LayerShellActions<()>>>();

let mut instance = Box::pin(run_instance::<A, E, C>(
application,
Expand Down Expand Up @@ -250,16 +250,16 @@ where
LayerShellActions::CustomActions(actions) => {
for action in actions {
match action {
LayershellCustomActions::AnchorChange(anchor) => {
LayershellCustomActionsWithInfo::AnchorChange(anchor) => {
ev.main_window().set_anchor(anchor);
}
LayershellCustomActions::LayerChange(layer) => {
LayershellCustomActionsWithInfo::LayerChange(layer) => {
ev.main_window().set_layer(layer);
}
LayershellCustomActions::SizeChange((width, height)) => {
LayershellCustomActionsWithInfo::SizeChange((width, height)) => {
ev.main_window().set_size((width, height));
}
LayershellCustomActions::VirtualKeyboardPressed {
LayershellCustomActionsWithInfo::VirtualKeyboardPressed {
time,
key,
} => {
Expand All @@ -279,6 +279,7 @@ where
)
.ok();
}
_ => {}
}
}
}
Expand Down Expand Up @@ -319,8 +320,8 @@ async fn run_instance<A, E, C>(
mut runtime: Runtime<E, IcedProxy<A::Message>, A::Message>,
mut proxy: IcedProxy<A::Message>,
mut debug: Debug,
mut event_receiver: mpsc::UnboundedReceiver<IcedLayerEvent<A::Message>>,
mut control_sender: mpsc::UnboundedSender<Vec<LayerShellActions>>,
mut event_receiver: mpsc::UnboundedReceiver<IcedLayerEvent<A::Message, ()>>,
mut control_sender: mpsc::UnboundedSender<Vec<LayerShellActions<()>>>,
mut state: State<A>,
init_command: Command<A::Message>,
window: Arc<WindowWrapper>,
Expand Down Expand Up @@ -558,7 +559,7 @@ pub(crate) fn update<A: Application, C, E: Executor>(
proxy: &mut IcedProxy<A::Message>,
debug: &mut Debug,
messages: &mut Vec<A::Message>,
custom_actions: &mut Vec<LayerShellActions>,
custom_actions: &mut Vec<LayerShellActions<()>>,
) where
C: Compositor<Renderer = A::Renderer> + 'static,
A::Theme: StyleSheet,
Expand Down Expand Up @@ -603,7 +604,7 @@ pub(crate) fn run_command<A, C, E>(
renderer: &mut A::Renderer,
command: Command<A::Message>,
runtime: &mut Runtime<E, IcedProxy<A::Message>, A::Message>,
custom_actions: &mut Vec<LayerShellActions>,
custom_actions: &mut Vec<LayerShellActions<()>>,
should_exit: &mut bool,
proxy: &mut IcedProxy<A::Message>,
debug: &mut Debug,
Expand Down Expand Up @@ -685,7 +686,7 @@ pub(crate) fn run_command<A, C, E>(
proxy.send(tagger(Ok(())));
}
command::Action::Custom(custom) => {
if let Some(action) = custom.downcast_ref::<LayershellCustomActions>() {
if let Some(action) = custom.downcast_ref::<LayershellCustomActionsWithInfo<()>>() {
customactions.push(*action);
}
}
Expand Down
2 changes: 1 addition & 1 deletion iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<A: Application> State<A>
where
A::Theme: application::StyleSheet,
{
pub fn new(application: &A, window: &layershellev::WindowState<()>) -> Self {
pub fn new(application: &A, window: &layershellev::WindowState<(), ()>) -> Self {
let scale_factor = application.scale_factor();
let theme = application.theme();
let appearance = theme.appearance(&application.style());
Expand Down
29 changes: 18 additions & 11 deletions iced_layershell/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ pub enum WindowEvent {
}

#[derive(Debug)]
pub enum IcedLayerEvent<Message: 'static> {
pub enum IcedLayerEvent<Message: 'static, INFO: Clone> {
RequestRefreshWithWrapper {
width: u32,
height: u32,
wrapper: WindowWrapper,
is_created: bool,
info: Option<INFO>,
},
RequestRefresh {
width: u32,
Expand All @@ -78,23 +80,28 @@ pub enum IcedLayerEvent<Message: 'static> {

#[allow(unused)]
#[derive(Debug)]
pub struct MultiWindowIcedLayerEvent<Message: 'static>(pub Option<Id>, pub IcedLayerEvent<Message>);
pub struct MultiWindowIcedLayerEvent<Message: 'static, INFO: Clone>(
pub Option<Id>,
pub IcedLayerEvent<Message, INFO>,
);

impl<Message: 'static> From<(Option<Id>, IcedLayerEvent<Message>)>
for MultiWindowIcedLayerEvent<Message>
impl<Message: 'static, INFO: Clone> From<(Option<Id>, IcedLayerEvent<Message, INFO>)>
for MultiWindowIcedLayerEvent<Message, INFO>
{
fn from((id, message): (Option<Id>, IcedLayerEvent<Message>)) -> Self {
fn from((id, message): (Option<Id>, IcedLayerEvent<Message, INFO>)) -> Self {
MultiWindowIcedLayerEvent(id, message)
}
}

impl<Message: 'static> From<&DispatchMessage> for IcedLayerEvent<Message> {
fn from(value: &DispatchMessage) -> Self {
impl<Message: 'static, INFO: Clone> From<&DispatchMessage<INFO>> for IcedLayerEvent<Message, INFO> {
fn from(value: &DispatchMessage<INFO>) -> Self {
match value {
DispatchMessage::RequestRefresh { width, height } => IcedLayerEvent::RequestRefresh {
width: *width,
height: *height,
},
DispatchMessage::RequestRefresh { width, height, .. } => {
IcedLayerEvent::RequestRefresh {
width: *width,
height: *height,
}
}
DispatchMessage::MouseEnter {
surface_x: x,
surface_y: y,
Expand Down
8 changes: 8 additions & 0 deletions iced_layershell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod reexport {
pub use layershellev::reexport::Anchor;
pub use layershellev::reexport::KeyboardInteractivity;
pub use layershellev::reexport::Layer;
pub use layershellev::NewLayerShellSettings;
}

use settings::Settings;
Expand Down Expand Up @@ -243,6 +244,8 @@ pub trait MultiApplication: Sized {
fn id_info(&self, _id: iced_core::window::Id) -> Option<Self::WindowInfo> {
None
}

fn set_id_info(&mut self, _id: iced_core::window::Id, _info: Self::WindowInfo) {}
/// Handles a __message__ and updates the state of the [`Application`].
///
/// This is where you define your __update logic__. All the __messages__,
Expand Down Expand Up @@ -313,6 +316,7 @@ pub trait MultiApplication: Sized {
fn run(settings: Settings<Self::Flags>) -> Result<(), error::Error>
where
Self: 'static,
<Self as MultiApplication>::WindowInfo: Clone,
{
#[allow(clippy::needless_update)]
let renderer_settings = iced_renderer::Settings {
Expand Down Expand Up @@ -392,4 +396,8 @@ where
fn id_info(&self, id: iced_core::window::Id) -> Option<Self::WindowInfo> {
self.0.id_info(id)
}

fn set_id_info(&mut self, id: iced_core::window::Id, info: Self::WindowInfo) {
self.0.set_id_info(id, info)
}
}
Loading

0 comments on commit dda9c44

Please sign in to comment.