@@ -11,7 +11,7 @@ use cosmic::{
1111 cosmic_config:: { self , ConfigSet , CosmicConfigEntry } ,
1212 cosmic_theme:: Spacing ,
1313 iced:: {
14- Task ,
14+ Rectangle , Task ,
1515 platform_specific:: shell:: commands:: popup:: { destroy_popup, get_popup} ,
1616 widget:: { column, row} ,
1717 window:: Id ,
@@ -20,11 +20,17 @@ use cosmic::{
2020 iced_runtime:: { Appearance , core:: window} ,
2121 prelude:: * ,
2222 surface, theme,
23- widget:: { self , horizontal_space, vertical_space} ,
23+ widget:: {
24+ self , autosize, horizontal_space,
25+ rectangle_tracker:: { RectangleTracker , RectangleUpdate , rectangle_tracker_subscription} ,
26+ vertical_space,
27+ } ,
2428} ;
2529use cosmic_comp_config:: CosmicCompConfig ;
30+ use std:: sync:: LazyLock ;
2631use xkb_data:: KeyboardLayout ;
2732
33+ static AUTOSIZE_MAIN_ID : LazyLock < widget:: Id > = LazyLock :: new ( || widget:: Id :: new ( "autosize-main" ) ) ;
2834pub const ID : & str = "com.system76.CosmicAppletInputSources" ;
2935
3036pub fn run ( ) -> cosmic:: iced:: Result {
@@ -77,6 +83,8 @@ pub struct Window {
7783 comp_config_handler : Option < cosmic_config:: Config > ,
7884 layouts : Vec < KeyboardLayout > ,
7985 active_layouts : Vec < ActiveLayout > ,
86+ rectangle_tracker : Option < RectangleTracker < u32 > > ,
87+ rectangle : Rectangle ,
8088}
8189
8290#[ derive( Clone , Debug ) ]
@@ -87,6 +95,7 @@ pub enum Message {
8795 SetActiveLayout ( usize ) ,
8896 KeyboardSettings ,
8997 Surface ( surface:: Action ) ,
98+ Rectangle ( RectangleUpdate < u32 > ) ,
9099}
91100
92101#[ derive( Debug ) ]
@@ -119,6 +128,8 @@ impl cosmic::Application for Window {
119128 popup : None ,
120129 comp_config : flags. comp_config ,
121130 active_layouts : Vec :: new ( ) ,
131+ rectangle_tracker : None ,
132+ rectangle : Rectangle :: default ( ) ,
122133 } ;
123134 ( window, Task :: none ( ) )
124135 }
@@ -194,6 +205,14 @@ impl cosmic::Application for Window {
194205 cosmic:: app:: Action :: Surface ( a) ,
195206 ) ) ;
196207 }
208+ Message :: Rectangle ( u) => match u {
209+ RectangleUpdate :: Rectangle ( r) => {
210+ self . rectangle = r. 1 ;
211+ }
212+ RectangleUpdate :: Init ( tracker) => {
213+ self . rectangle_tracker = Some ( tracker) ;
214+ }
215+ } ,
197216 }
198217
199218 Task :: none ( )
@@ -205,11 +224,19 @@ impl cosmic::Application for Window {
205224 . first ( )
206225 . map_or ( "" , |l| l. layout . as_str ( ) ) ,
207226 ) ;
208-
209- self . core
227+ let button = self
228+ . core
210229 . applet
211- . text_button ( input_source_text, Message :: TogglePopup )
212- . into ( )
230+ . text_button ( input_source_text, Message :: TogglePopup ) ;
231+ autosize:: autosize (
232+ if let Some ( tracker) = self . rectangle_tracker . as_ref ( ) {
233+ Element :: from ( tracker. container ( 0 , button) . ignore_bounds ( true ) )
234+ } else {
235+ button. into ( )
236+ } ,
237+ AUTOSIZE_MAIN_ID . clone ( ) ,
238+ )
239+ . into ( )
213240 }
214241
215242 fn view_window ( & self , _id : Id ) -> Element < ' _ , Self :: Message > {
@@ -242,18 +269,21 @@ impl cosmic::Application for Window {
242269 }
243270
244271 fn subscription ( & self ) -> Subscription < Self :: Message > {
245- self . core
246- . watch_config ( "com.system76.CosmicComp" )
247- . map ( |update| {
248- if !update. errors . is_empty ( ) {
249- tracing:: error!(
250- "errors loading config {:?}: {:?}" ,
251- update. keys,
252- update. errors
253- ) ;
254- }
255- Message :: CompConfig ( Box :: new ( update. config ) )
256- } )
272+ Subscription :: batch ( vec ! [
273+ rectangle_tracker_subscription( 0 ) . map( |e| Message :: Rectangle ( e. 1 ) ) ,
274+ self . core
275+ . watch_config( "com.system76.CosmicComp" )
276+ . map( |update| {
277+ if !update. errors. is_empty( ) {
278+ tracing:: error!(
279+ "errors loading config {:?}: {:?}" ,
280+ update. keys,
281+ update. errors
282+ ) ;
283+ }
284+ Message :: CompConfig ( Box :: new( update. config) )
285+ } ) ,
286+ ] )
257287 }
258288
259289 fn style ( & self ) -> Option < Appearance > {
0 commit comments