@@ -6,9 +6,12 @@ use crate::modules::{
66} ;
77use crate :: { glib_recv, lock, module_impl, send_async, spawn, try_send} ;
88use glib:: Propagation ;
9+ use gtk:: gdk:: { EventMask , ScrollDirection } ;
910use gtk:: pango:: EllipsizeMode ;
1011use gtk:: prelude:: * ;
11- use gtk:: { Button , CellRendererText , ComboBoxText , Label , Orientation , Scale , ToggleButton } ;
12+ use gtk:: {
13+ Button , CellRendererText , ComboBoxText , EventBox , Label , Orientation , Scale , ToggleButton ,
14+ } ;
1215use serde:: Deserialize ;
1316use std:: collections:: HashMap ;
1417use tokio:: sync:: mpsc;
@@ -128,9 +131,11 @@ pub enum Update {
128131
129132 InputVolume ( u32 , f64 ) ,
130133 InputMute ( u32 , bool ) ,
134+ InputVolumeUp ( f64 ) ,
135+ InputVolumeDown ( f64 ) ,
131136}
132137
133- impl Module < Button > for VolumeModule {
138+ impl Module < EventBox > for VolumeModule {
134139 type SendMessage = Event ;
135140 type ReceiveMessage = Update ;
136141
@@ -143,7 +148,7 @@ impl Module<Button> for VolumeModule {
143148 mut rx : mpsc:: Receiver < Self :: ReceiveMessage > ,
144149 ) -> color_eyre:: Result < ( ) >
145150 where
146- <Self as Module < Button > >:: SendMessage : Clone ,
151+ <Self as Module < EventBox > >:: SendMessage : Clone ,
147152 {
148153 let client = context. client :: < volume:: Client > ( ) ;
149154
@@ -197,6 +202,8 @@ impl Module<Button> for VolumeModule {
197202 Update :: SinkVolume ( name, volume) => client. set_sink_volume ( & name, volume) ,
198203 Update :: SinkMute ( name, muted) => client. set_sink_muted ( & name, muted) ,
199204 Update :: InputVolume ( index, volume) => client. set_input_volume ( index, volume) ,
205+ Update :: InputVolumeUp ( val) => client. set_default_volume ( val) ,
206+ Update :: InputVolumeDown ( val) => client. set_default_volume ( val) ,
200207 Update :: InputMute ( index, muted) => client. set_input_muted ( index, muted) ,
201208 }
202209 }
@@ -209,17 +216,17 @@ impl Module<Button> for VolumeModule {
209216 self ,
210217 context : WidgetContext < Self :: SendMessage , Self :: ReceiveMessage > ,
211218 info : & ModuleInfo ,
212- ) -> color_eyre:: Result < ModuleParts < Button > >
219+ ) -> color_eyre:: Result < ModuleParts < EventBox > >
213220 where
214- <Self as Module < Button > >:: SendMessage : Clone ,
221+ <Self as Module < EventBox > >:: SendMessage : Clone ,
215222 {
216223 let button_label = Label :: builder ( )
217224 . use_markup ( true )
218225 . angle ( self . layout . angle ( info) )
219226 . justify ( self . layout . justify . into ( ) )
220227 . build ( ) ;
221228
222- let button = Button :: new ( ) ;
229+ let button = Button :: builder ( ) . build ( ) ;
223230 button. add ( & button_label) ;
224231
225232 {
@@ -251,6 +258,49 @@ impl Module<Button> for VolumeModule {
251258 } ) ;
252259 }
253260
261+ let event_box = EventBox :: builder ( )
262+ . events (
263+ EventMask :: SCROLL_MASK
264+ | EventMask :: SMOOTH_SCROLL_MASK
265+ | EventMask :: BUTTON_MOTION_MASK ,
266+ )
267+ . child ( & button)
268+ . build ( ) ;
269+
270+ {
271+ let tx = context. tx . clone ( ) ;
272+ event_box. connect_scroll_event ( move |_button, scroll| {
273+ match scroll. direction ( ) {
274+ ScrollDirection :: Up => {
275+ try_send ! (
276+ tx,
277+ ModuleUpdateEvent :: Update ( Event :: VolumeUp ( scroll. delta( ) . 1 ) )
278+ ) ;
279+ }
280+ ScrollDirection :: Down => {
281+ try_send ! (
282+ tx,
283+ ModuleUpdateEvent :: Update ( Event :: VolumeDown ( scroll. delta( ) . 1 ) )
284+ ) ;
285+ }
286+ ScrollDirection :: Smooth => {
287+ if scroll. scroll_deltas ( ) . unwrap_or_default ( ) . 1 > 0.0 {
288+ try_send ! (
289+ tx,
290+ ModuleUpdateEvent :: Update ( Event :: VolumeUp ( scroll. delta( ) . 1 ) )
291+ ) ;
292+ } else {
293+ try_send ! (
294+ tx,
295+ ModuleUpdateEvent :: Update ( Event :: VolumeDown ( scroll. delta( ) . 1 ) )
296+ ) ;
297+ }
298+ }
299+ _ => { }
300+ }
301+ Propagation :: Stop
302+ } ) ;
303+ }
254304 let popup = self
255305 . into_popup (
256306 context. controller_tx . clone ( ) ,
@@ -260,7 +310,7 @@ impl Module<Button> for VolumeModule {
260310 )
261311 . into_popup_parts ( vec ! [ & button] ) ;
262312
263- Ok ( ModuleParts :: new ( button , popup) )
313+ Ok ( ModuleParts :: new ( event_box , popup) )
264314 }
265315
266316 fn into_popup (
@@ -460,6 +510,13 @@ impl Module<Button> for VolumeModule {
460510 input_container. remove( & ui. container) ;
461511 }
462512 }
513+ Event :: VolumeUp ( val) => {
514+ try_send!( tx, Update :: InputVolumeUp ( val) ) ;
515+ }
516+
517+ Event :: VolumeDown ( val) => {
518+ try_send!( tx, Update :: InputVolumeDown ( val) ) ;
519+ }
463520 }
464521 } ) ;
465522 }
0 commit comments