Skip to content

Commit 69d71d8

Browse files
authored
feat: add iced::window:Id in theme and style callbacks of multi_window. (#141)
Signed-off-by: fortime <palfortime@gmail.com>
1 parent 8c3ea37 commit 69d71d8

File tree

4 files changed

+85
-50
lines changed

4 files changed

+85
-50
lines changed

iced_layershell/src/build_pattern/daemon.rs

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,19 @@ pub trait Program: Sized {
7373
/// Returns the current [`Theme`] of the [`Application`].
7474
///
7575
/// [`Theme`]: Self::Theme
76-
fn theme(&self, _state: &Self::State) -> Self::Theme {
76+
fn theme(&self, _state: &Self::State, _id: iced_core::window::Id) -> Self::Theme {
7777
Self::Theme::default()
7878
}
7979

8080
/// Returns the current `Style` of the [`Theme`].
8181
///
8282
/// [`Theme`]: Self::Theme
83-
fn style(&self, _state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
83+
fn style(
84+
&self,
85+
_state: &Self::State,
86+
theme: &Self::Theme,
87+
_id: iced_core::window::Id,
88+
) -> crate::Appearance {
8489
theme.default_style()
8590
}
8691

@@ -169,12 +174,12 @@ pub trait Program: Sized {
169174
self.program.subscription(&self.state)
170175
}
171176

172-
fn theme(&self) -> Self::Theme {
173-
self.program.theme(&self.state)
177+
fn theme(&self, id: iced_core::window::Id) -> Self::Theme {
178+
self.program.theme(&self.state, id)
174179
}
175180

176-
fn style(&self, theme: &Self::Theme) -> crate::Appearance {
177-
self.program.style(&self.state, theme)
181+
fn style(&self, theme: &Self::Theme, id: iced_core::window::Id) -> crate::Appearance {
182+
self.program.style(&self.state, theme, id)
178183
}
179184

180185
fn scale_factor(&self, window: iced_core::window::Id) -> f64 {
@@ -421,16 +426,21 @@ fn with_namespace<P: Program>(
421426
self.program.view(state, window)
422427
}
423428

424-
fn theme(&self, state: &Self::State) -> Self::Theme {
425-
self.program.theme(state)
429+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
430+
self.program.theme(state, id)
426431
}
427432

428433
fn subscription(&self, state: &Self::State) -> iced::Subscription<Self::Message> {
429434
self.program.subscription(state)
430435
}
431436

432-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
433-
self.program.style(state, theme)
437+
fn style(
438+
&self,
439+
state: &Self::State,
440+
theme: &Self::Theme,
441+
id: iced_core::window::Id,
442+
) -> crate::Appearance {
443+
self.program.style(state, theme, id)
434444
}
435445

436446
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -482,12 +492,17 @@ pub fn with_subscription<P: Program>(
482492
self.program.namespace(state)
483493
}
484494

485-
fn theme(&self, state: &Self::State) -> Self::Theme {
486-
self.program.theme(state)
495+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
496+
self.program.theme(state, id)
487497
}
488498

489-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
490-
self.program.style(state, theme)
499+
fn style(
500+
&self,
501+
state: &Self::State,
502+
theme: &Self::Theme,
503+
id: iced_core::window::Id,
504+
) -> crate::Appearance {
505+
self.program.style(state, theme, id)
491506
}
492507

493508
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -503,7 +518,7 @@ pub fn with_subscription<P: Program>(
503518

504519
pub fn with_theme<P: Program>(
505520
program: P,
506-
f: impl Fn(&P::State) -> P::Theme,
521+
f: impl Fn(&P::State, iced_core::window::Id) -> P::Theme,
507522
) -> impl Program<State = P::State, Message = P::Message, Theme = P::Theme> {
508523
struct WithTheme<P, F> {
509524
program: P,
@@ -512,16 +527,16 @@ pub fn with_theme<P: Program>(
512527

513528
impl<P: Program, F> Program for WithTheme<P, F>
514529
where
515-
F: Fn(&P::State) -> P::Theme,
530+
F: Fn(&P::State, iced_core::window::Id) -> P::Theme,
516531
{
517532
type State = P::State;
518533
type Message = P::Message;
519534
type Theme = P::Theme;
520535
type Renderer = P::Renderer;
521536
type Executor = P::Executor;
522537

523-
fn theme(&self, state: &Self::State) -> Self::Theme {
524-
(self.theme)(state)
538+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
539+
(self.theme)(state, id)
525540
}
526541
fn remove_id(&self, state: &mut Self::State, id: iced_core::window::Id) {
527542
self.program.remove_id(state, id)
@@ -546,8 +561,13 @@ pub fn with_theme<P: Program>(
546561
self.program.subscription(state)
547562
}
548563

549-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
550-
self.program.style(state, theme)
564+
fn style(
565+
&self,
566+
state: &Self::State,
567+
theme: &Self::Theme,
568+
id: iced_core::window::Id,
569+
) -> crate::Appearance {
570+
self.program.style(state, theme, id)
551571
}
552572

553573
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -560,7 +580,7 @@ pub fn with_theme<P: Program>(
560580

561581
pub fn with_style<P: Program>(
562582
program: P,
563-
f: impl Fn(&P::State, &P::Theme) -> crate::Appearance,
583+
f: impl Fn(&P::State, &P::Theme, iced_core::window::Id) -> crate::Appearance,
564584
) -> impl Program<State = P::State, Message = P::Message, Theme = P::Theme> {
565585
struct WithStyle<P, F> {
566586
program: P,
@@ -569,16 +589,21 @@ pub fn with_style<P: Program>(
569589

570590
impl<P: Program, F> Program for WithStyle<P, F>
571591
where
572-
F: Fn(&P::State, &P::Theme) -> crate::Appearance,
592+
F: Fn(&P::State, &P::Theme, iced_core::window::Id) -> crate::Appearance,
573593
{
574594
type State = P::State;
575595
type Message = P::Message;
576596
type Theme = P::Theme;
577597
type Renderer = P::Renderer;
578598
type Executor = P::Executor;
579599

580-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
581-
(self.style)(state, theme)
600+
fn style(
601+
&self,
602+
state: &Self::State,
603+
theme: &Self::Theme,
604+
id: iced_core::window::Id,
605+
) -> crate::Appearance {
606+
(self.style)(state, theme, id)
582607
}
583608

584609
fn namespace(&self, state: &Self::State) -> String {
@@ -603,8 +628,8 @@ pub fn with_style<P: Program>(
603628
self.program.subscription(state)
604629
}
605630

606-
fn theme(&self, state: &Self::State) -> Self::Theme {
607-
self.program.theme(state)
631+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
632+
self.program.theme(state, id)
608633
}
609634

610635
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -656,12 +681,17 @@ pub fn with_scale_factor<P: Program>(
656681
self.program.subscription(state)
657682
}
658683

659-
fn theme(&self, state: &Self::State) -> Self::Theme {
660-
self.program.theme(state)
684+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
685+
self.program.theme(state, id)
661686
}
662687

663-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
664-
self.program.style(state, theme)
688+
fn style(
689+
&self,
690+
state: &Self::State,
691+
theme: &Self::Theme,
692+
id: iced_core::window::Id,
693+
) -> crate::Appearance {
694+
self.program.style(state, theme, id)
665695
}
666696

667697
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -717,12 +747,17 @@ pub fn with_executor<P: Program, E: iced_futures::Executor>(
717747
self.program.subscription(state)
718748
}
719749

720-
fn theme(&self, state: &Self::State) -> Self::Theme {
721-
self.program.theme(state)
750+
fn theme(&self, state: &Self::State, id: iced_core::window::Id) -> Self::Theme {
751+
self.program.theme(state, id)
722752
}
723753

724-
fn style(&self, state: &Self::State, theme: &Self::Theme) -> crate::Appearance {
725-
self.program.style(state, theme)
754+
fn style(
755+
&self,
756+
state: &Self::State,
757+
theme: &Self::Theme,
758+
id: iced_core::window::Id,
759+
) -> crate::Appearance {
760+
self.program.style(state, theme, id)
726761
}
727762

728763
fn scale_factor(&self, state: &Self::State, window: iced_core::window::Id) -> f64 {
@@ -816,7 +851,7 @@ impl<P: Program> Daemon<P> {
816851
/// Sets the style logic of the [`Application`].
817852
pub fn style(
818853
self,
819-
f: impl Fn(&P::State, &P::Theme) -> crate::Appearance,
854+
f: impl Fn(&P::State, &P::Theme, iced_core::window::Id) -> crate::Appearance,
820855
) -> Daemon<impl Program<State = P::State, Message = P::Message, Theme = P::Theme>> {
821856
Daemon {
822857
raw: with_style(self.raw, f),
@@ -837,7 +872,7 @@ impl<P: Program> Daemon<P> {
837872
/// Sets the theme logic of the [`Application`].
838873
pub fn theme(
839874
self,
840-
f: impl Fn(&P::State) -> P::Theme,
875+
f: impl Fn(&P::State, iced_core::window::Id) -> P::Theme,
841876
) -> Daemon<impl Program<State = P::State, Message = P::Message, Theme = P::Theme>> {
842877
Daemon {
843878
raw: with_theme(self.raw, f),

iced_layershell/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,14 @@ pub trait MultiApplication: Sized {
300300
///
301301
/// [`Theme`]: Self::Theme
302302
#[allow(unused_variables)]
303-
fn theme(&self) -> Self::Theme {
303+
fn theme(&self, _id: iced_core::window::Id) -> Self::Theme {
304304
Self::Theme::default()
305305
}
306306

307307
/// Returns the current `Style` of the [`Theme`].
308308
///
309309
/// [`Theme`]: Self::Theme
310-
fn style(&self, theme: &Self::Theme) -> Appearance {
310+
fn style(&self, theme: &Self::Theme, _id: iced_core::window::Id) -> Appearance {
311311
theme.default_style()
312312
}
313313

@@ -408,12 +408,12 @@ where
408408
self.0.namespace()
409409
}
410410

411-
fn theme(&self) -> A::Theme {
412-
self.0.theme()
411+
fn theme(&self, id: iced_core::window::Id) -> A::Theme {
412+
self.0.theme(id)
413413
}
414414

415-
fn style(&self, theme: &Self::Theme) -> Appearance {
416-
self.0.style(theme)
415+
fn style(&self, theme: &Self::Theme, id: iced_core::window::Id) -> Appearance {
416+
self.0.style(theme, id)
417417
}
418418

419419
fn subscription(&self) -> Subscription<Self::Message> {

iced_layershell/src/multi_window.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ where
8181
///
8282
/// This title can be dynamic! The runtime will automatically update the
8383
/// title of your application when necessary.
84-
fn title(&self) -> String {
84+
fn title(&self, _id: iced_core::window::Id) -> String {
8585
self.namespace()
8686
}
8787

8888
fn remove_id(&mut self, _id: iced_core::window::Id);
8989
/// Returns the current `Theme` of the [`Application`].
90-
fn theme(&self) -> Self::Theme;
90+
fn theme(&self, id: iced_core::window::Id) -> Self::Theme;
9191

9292
/// Returns the `Style` variation of the `Theme`.
93-
fn style(&self, theme: &Self::Theme) -> Appearance {
93+
fn style(&self, theme: &Self::Theme, _id: iced_core::window::Id) -> Appearance {
9494
theme.default_style()
9595
}
9696

@@ -712,7 +712,7 @@ async fn run_instance<A, E, C>(
712712
debug.draw_started();
713713
ui.draw(
714714
&mut window.renderer,
715-
&application.theme(),
715+
&application.theme(id),
716716
&iced_core::renderer::Style {
717717
text_color: window.state.text_color(),
718718
},

iced_layershell/src/multi_window/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ where
3737
window: &WindowWrapper,
3838
) -> Self {
3939
let application_scale_factor = application.scale_factor(id);
40-
let theme = application.theme();
41-
let appearance = application.style(&theme);
40+
let theme = application.theme(id);
41+
let appearance = application.style(&theme, id);
4242

4343
let logical_size = Size::new(width, height);
4444
let viewport = viewport(logical_size, wayland_scale_factor, application_scale_factor);
@@ -143,8 +143,8 @@ where
143143
self.application_scale_factor = new_scale_factor;
144144
self.resize_viewport(self.logical_size_u32());
145145
}
146-
self.theme = application.theme();
147-
self.appearance = application.style(&self.theme);
146+
self.theme = application.theme(self.id);
147+
self.appearance = application.style(&self.theme, self.id);
148148
}
149149

150150
fn resize_viewport(&mut self, logical_size: Size<u32>) {

0 commit comments

Comments
 (0)