@@ -7,9 +7,12 @@ use crate::modules::{
77} ;
88use crate :: { lock, module_impl, spawn} ;
99use glib:: Propagation ;
10+ use gtk:: gdk:: { EventMask , ScrollDirection } ;
1011use gtk:: pango:: EllipsizeMode ;
1112use gtk:: prelude:: * ;
12- use gtk:: { Button , CellRendererText , ComboBoxText , Label , Orientation , Scale , ToggleButton } ;
13+ use gtk:: {
14+ Button , CellRendererText , ComboBoxText , EventBox , Label , Orientation , Scale , ToggleButton ,
15+ } ;
1316use serde:: Deserialize ;
1417use std:: collections:: HashMap ;
1518use tokio:: sync:: mpsc;
@@ -134,9 +137,11 @@ pub enum Update {
134137
135138 InputVolume ( u32 , f64 ) ,
136139 InputMute ( u32 , bool ) ,
140+ InputVolumeUp ( f64 ) ,
141+ InputVolumeDown ( f64 ) ,
137142}
138143
139- impl Module < Button > for VolumeModule {
144+ impl Module < EventBox > for VolumeModule {
140145 type SendMessage = Event ;
141146 type ReceiveMessage = Update ;
142147
@@ -149,7 +154,7 @@ impl Module<Button> for VolumeModule {
149154 mut rx : mpsc:: Receiver < Self :: ReceiveMessage > ,
150155 ) -> color_eyre:: Result < ( ) >
151156 where
152- <Self as Module < Button > >:: SendMessage : Clone ,
157+ <Self as Module < EventBox > >:: SendMessage : Clone ,
153158 {
154159 let client = context. client :: < volume:: Client > ( ) ;
155160
@@ -200,6 +205,8 @@ impl Module<Button> for VolumeModule {
200205 Update :: SinkVolume ( name, volume) => client. set_sink_volume ( & name, volume) ,
201206 Update :: SinkMute ( name, muted) => client. set_sink_muted ( & name, muted) ,
202207 Update :: InputVolume ( index, volume) => client. set_input_volume ( index, volume) ,
208+ Update :: InputVolumeUp ( val) => client. set_default_volume ( val) ,
209+ Update :: InputVolumeDown ( val) => client. set_default_volume ( val) ,
203210 Update :: InputMute ( index, muted) => client. set_input_muted ( index, muted) ,
204211 }
205212 }
@@ -212,17 +219,17 @@ impl Module<Button> for VolumeModule {
212219 self ,
213220 context : WidgetContext < Self :: SendMessage , Self :: ReceiveMessage > ,
214221 info : & ModuleInfo ,
215- ) -> color_eyre:: Result < ModuleParts < Button > >
222+ ) -> color_eyre:: Result < ModuleParts < EventBox > >
216223 where
217- <Self as Module < Button > >:: SendMessage : Clone ,
224+ <Self as Module < EventBox > >:: SendMessage : Clone ,
218225 {
219226 let button_label = Label :: builder ( )
220227 . use_markup ( true )
221228 . angle ( self . layout . angle ( info) )
222229 . justify ( self . layout . justify . into ( ) )
223230 . build ( ) ;
224231
225- let button = Button :: new ( ) ;
232+ let button = Button :: builder ( ) . build ( ) ;
226233 button. add ( & button_label) ;
227234
228235 {
@@ -257,11 +264,42 @@ impl Module<Button> for VolumeModule {
257264 } ,
258265 ) ;
259266
267+ let event_box = EventBox :: builder ( )
268+ . events (
269+ EventMask :: SCROLL_MASK
270+ | EventMask :: SMOOTH_SCROLL_MASK
271+ | EventMask :: BUTTON_MOTION_MASK ,
272+ )
273+ . child ( & button)
274+ . build ( ) ;
275+
276+ {
277+ let tx = context. tx . clone ( ) ;
278+ event_box. connect_scroll_event ( move |_button, scroll| {
279+ match scroll. direction ( ) {
280+ ScrollDirection :: Up => {
281+ tx. send_update_spawn ( Event :: VolumeUp ( scroll. delta ( ) . 1 ) ) ;
282+ }
283+ ScrollDirection :: Down => {
284+ tx. send_update_spawn ( Event :: VolumeDown ( scroll. delta ( ) . 1 ) ) ;
285+ }
286+ ScrollDirection :: Smooth => {
287+ if scroll. scroll_deltas ( ) . unwrap_or_default ( ) . 1 > 0.0 {
288+ tx. send_update_spawn ( Event :: VolumeUp ( scroll. delta ( ) . 1 ) ) ;
289+ } else {
290+ tx. send_update_spawn ( Event :: VolumeDown ( scroll. delta ( ) . 1 ) ) ;
291+ }
292+ }
293+ _ => { }
294+ }
295+ Propagation :: Stop
296+ } ) ;
297+ }
260298 let popup = self
261299 . into_popup ( context, info)
262300 . into_popup_parts ( vec ! [ & button] ) ;
263301
264- Ok ( ModuleParts :: new ( button , popup) )
302+ Ok ( ModuleParts :: new ( event_box , popup) )
265303 }
266304
267305 fn into_popup (
@@ -356,6 +394,8 @@ impl Module<Button> for VolumeModule {
356394 let mut inputs = HashMap :: new ( ) ;
357395 let mut sinks = vec ! [ ] ;
358396
397+ let tx = context. controller_tx . clone ( ) ;
398+
359399 context
360400 . subscribe ( )
361401 . recv_glib ( & input_container, move |input_container, event| {
@@ -480,6 +520,13 @@ impl Module<Button> for VolumeModule {
480520 input_container. remove ( & ui. container ) ;
481521 }
482522 }
523+ Event :: VolumeUp ( val) => {
524+ tx. send_spawn ( Update :: InputVolumeUp ( val) ) ;
525+ }
526+
527+ Event :: VolumeDown ( val) => {
528+ tx. send_spawn ( Update :: InputVolumeDown ( val) ) ;
529+ }
483530 }
484531 } ) ;
485532
0 commit comments