1- use iced:: gradient;
2- use iced:: widget:: { column, container, horizontal_space, row, slider, text} ;
1+ use iced:: application;
2+ use iced:: theme:: { self , Theme } ;
3+ use iced:: widget:: {
4+ checkbox, column, container, horizontal_space, row, slider, text,
5+ } ;
6+ use iced:: { gradient, window} ;
37use iced:: {
48 Alignment , Background , Color , Element , Length , Radians , Sandbox , Settings ,
59} ;
610
711pub fn main ( ) -> iced:: Result {
8- Gradient :: run ( Settings :: default ( ) )
12+ tracing_subscriber:: fmt:: init ( ) ;
13+
14+ Gradient :: run ( Settings {
15+ window : window:: Settings {
16+ transparent : true ,
17+ ..Default :: default ( )
18+ } ,
19+ ..Default :: default ( )
20+ } )
921}
1022
1123#[ derive( Debug , Clone , Copy ) ]
1224struct Gradient {
1325 start : Color ,
1426 end : Color ,
1527 angle : Radians ,
28+ transparent : bool ,
1629}
1730
1831#[ derive( Debug , Clone , Copy ) ]
1932enum Message {
2033 StartChanged ( Color ) ,
2134 EndChanged ( Color ) ,
2235 AngleChanged ( Radians ) ,
36+ TransparentToggled ( bool ) ,
2337}
2438
2539impl Sandbox for Gradient {
@@ -30,6 +44,7 @@ impl Sandbox for Gradient {
3044 start : Color :: WHITE ,
3145 end : Color :: new ( 0.0 , 0.0 , 1.0 , 1.0 ) ,
3246 angle : Radians ( 0.0 ) ,
47+ transparent : false ,
3348 }
3449 }
3550
@@ -42,11 +57,19 @@ impl Sandbox for Gradient {
4257 Message :: StartChanged ( color) => self . start = color,
4358 Message :: EndChanged ( color) => self . end = color,
4459 Message :: AngleChanged ( angle) => self . angle = angle,
60+ Message :: TransparentToggled ( transparent) => {
61+ self . transparent = transparent;
62+ }
4563 }
4664 }
4765
4866 fn view ( & self ) -> Element < Message > {
49- let Self { start, end, angle } = * self ;
67+ let Self {
68+ start,
69+ end,
70+ angle,
71+ transparent,
72+ } = * self ;
5073
5174 let gradient_box = container ( horizontal_space ( Length :: Fill ) )
5275 . width ( Length :: Fill )
@@ -72,14 +95,34 @@ impl Sandbox for Gradient {
7295 . padding ( 8 )
7396 . align_items ( Alignment :: Center ) ;
7497
98+ let transparency_toggle = iced:: widget:: Container :: new (
99+ checkbox ( "Transparent window" , transparent)
100+ . on_toggle ( Message :: TransparentToggled ) ,
101+ )
102+ . padding ( 8 ) ;
103+
75104 column ! [
76105 color_picker( "Start" , self . start) . map( Message :: StartChanged ) ,
77106 color_picker( "End" , self . end) . map( Message :: EndChanged ) ,
78107 angle_picker,
79- gradient_box
108+ transparency_toggle,
109+ gradient_box,
80110 ]
81111 . into ( )
82112 }
113+
114+ fn style ( & self ) -> theme:: Application {
115+ if self . transparent {
116+ theme:: Application :: custom ( |theme : & Theme | {
117+ application:: Appearance {
118+ background_color : Color :: TRANSPARENT ,
119+ text_color : theme. palette ( ) . text ,
120+ }
121+ } )
122+ } else {
123+ theme:: Application :: Default
124+ }
125+ }
83126}
84127
85128fn color_picker ( label : & str , color : Color ) -> Element < ' _ , Color > {
@@ -91,6 +134,8 @@ fn color_picker(label: &str, color: Color) -> Element<'_, Color> {
91134 . step( 0.01 ) ,
92135 slider( 0.0 ..=1.0 , color. b, move |b| { Color { b, ..color } } )
93136 . step( 0.01 ) ,
137+ slider( 0.0 ..=1.0 , color. a, move |a| { Color { a, ..color } } )
138+ . step( 0.01 ) ,
94139 ]
95140 . spacing ( 8 )
96141 . padding ( 8 )
0 commit comments