1- use iced:: { Element , Length , Task , Theme } ;
1+ use iced:: { Element , Length , Subscription , Task , Theme , time , time :: Duration } ;
22use log:: { error, info} ;
33
44use crate :: gui:: components:: {
@@ -24,6 +24,8 @@ pub struct AmplifierApp {
2424 show_save_input : bool ,
2525 settings : Settings ,
2626 settings_dialog : SettingsDialog ,
27+
28+ dirty_chain : bool ,
2729}
2830
2931impl AmplifierApp {
@@ -53,7 +55,7 @@ impl AmplifierApp {
5355 let control_bar = Control :: new ( StageType :: default ( ) ) ;
5456 let settings_dialog = SettingsDialog :: new ( & settings. audio ) ;
5557
56- let app = Self {
58+ Self {
5759 processor_manager,
5860 stages,
5961 is_recording : false ,
@@ -66,43 +68,54 @@ impl AmplifierApp {
6668 show_save_input : false ,
6769 settings,
6870 settings_dialog,
69- } ;
7071
71- // Update the processor chain with the loaded preset
72- app. update_processor_chain ( ) ;
72+ // Set dirty chain to true to trigger initial rebuild
73+ dirty_chain : true ,
74+ }
75+ }
7376
74- app
77+ pub fn subscription ( & self ) -> Subscription < Message > {
78+ if self . dirty_chain {
79+ time:: every ( Duration :: from_millis ( 100 ) ) . map ( |_| Message :: RebuildTick )
80+ } else {
81+ Subscription :: none ( )
82+ }
7583 }
7684
7785 pub fn update ( & mut self , message : Message ) -> Task < Message > {
78- let mut should_update_chain = false ;
7986 let mut should_update_stages = false ;
8087
8188 match message {
89+ Message :: RebuildTick => {
90+ if self . dirty_chain {
91+ self . update_processor_chain ( ) ;
92+ self . dirty_chain = false ;
93+ }
94+ }
8295 Message :: AddStage => {
8396 let new_stage = StageConfig :: create_default ( self . control_bar . selected ( ) ) ;
8497 self . stages . push ( new_stage) ;
85- should_update_chain = true ;
98+ self . dirty_chain = true ;
8699 should_update_stages = true ;
87100 }
88101 Message :: RemoveStage ( idx) => {
89102 if idx < self . stages . len ( ) {
90103 self . stages . remove ( idx) ;
91104 }
92- should_update_chain = true ;
105+ self . dirty_chain = true ;
93106 should_update_stages = true ;
94107 }
95108 Message :: MoveStageUp ( idx) => {
96109 if idx > 0 {
97110 self . stages . swap ( idx - 1 , idx) ;
98- should_update_chain = true ;
111+ self . dirty_chain = true ;
99112 should_update_stages = true ;
100113 }
101114 }
102115 Message :: MoveStageDown ( idx) => {
103116 if idx < self . stages . len ( ) . saturating_sub ( 1 ) {
104117 self . stages . swap ( idx, idx + 1 ) ;
105- should_update_chain = true ;
118+ self . dirty_chain = true ;
106119 should_update_stages = true ;
107120 }
108121 }
@@ -115,7 +128,7 @@ impl AmplifierApp {
115128 self . selected_preset = Some ( preset_name. clone ( ) ) ;
116129 self . preset_bar
117130 . set_selected_preset ( Some ( preset_name. clone ( ) ) ) ;
118- should_update_chain = true ;
131+ self . dirty_chain = true ;
119132 should_update_stages = true ;
120133 info ! ( "Loaded preset: {preset_name}" ) ;
121134 }
@@ -192,7 +205,7 @@ impl AmplifierApp {
192205 self . stages . clear ( ) ;
193206 }
194207
195- should_update_chain = true ;
208+ self . dirty_chain = true ;
196209 }
197210 }
198211 Err ( e) => {
@@ -286,16 +299,12 @@ impl AmplifierApp {
286299 }
287300 Message :: Stage ( idx, stage_msg) => {
288301 if self . update_stage ( idx, stage_msg) {
289- should_update_chain = true ;
302+ self . dirty_chain = true ;
290303 should_update_stages = true
291304 }
292305 }
293306 }
294307
295- if should_update_chain {
296- self . update_processor_chain ( ) ;
297- }
298-
299308 if should_update_stages {
300309 self . stage_list . set_stages ( & self . stages ) ;
301310 }
0 commit comments