-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show volume controls on mobile
implementation from amberol fix #199
- Loading branch information
Showing
8 changed files
with
220 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,8 @@ template $MuzikaNPCover : Adw.Bin { | |
] | ||
} | ||
} | ||
|
||
$MuzikaNPVolumeControl {} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Gtk 4.0; | ||
|
||
template $MuzikaNPVolumeControl: Widget { | ||
Button volume_low_button { | ||
icon-name: 'audio-volume-low-symbolic'; | ||
action-name: 'volume.toggle-mute'; | ||
valign: center; | ||
|
||
styles [ | ||
"flat", | ||
"circular", | ||
] | ||
} | ||
|
||
Scale volume_scale { | ||
hexpand: true; | ||
margin-end: 6; | ||
adjustment: Adjustment volume_adjustment { | ||
lower: 0; | ||
upper: 1; | ||
step-increment: 0.05; | ||
value: 1; | ||
|
||
notify::value => $adjustment_value_changed_cb(); | ||
}; | ||
|
||
EventControllerScroll { | ||
name: "volume-scroll"; | ||
flags: vertical; | ||
|
||
scroll => $volume_scale_scrolled_cb(); | ||
} | ||
} | ||
|
||
Image volume_high_image { | ||
icon-name: 'audio-volume-high-symbolic'; | ||
margin-end: 10; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// from: https://gitlab.gnome.org/World/amberol/-/blob/main/src/volume_control.rs | ||
|
||
import GObject from "gi://GObject"; | ||
import Gtk from "gi://Gtk?version=4.0"; | ||
|
||
import { get_player } from "src/application"; | ||
import { SignalListeners } from "src/util/signal-listener"; | ||
import { get_volume_icon_name } from "src/util/volume"; | ||
|
||
export class MuzikaNPVolumeControl extends Gtk.Widget { | ||
static { | ||
GObject.registerClass( | ||
{ | ||
GTypeName: "MuzikaNPVolumeControl", | ||
Template: | ||
"resource:///com/vixalien/muzika/ui/components/player/now-playing/volume-control.ui", | ||
CssName: "np-volume", | ||
Properties: { | ||
volume: GObject.param_spec_double( | ||
"volume", | ||
"volume", | ||
"Volume", | ||
0.0, | ||
1.0, | ||
1.0, | ||
GObject.ParamFlags.READWRITE, | ||
), | ||
muted: GObject.param_spec_boolean( | ||
"muted", | ||
"muted", | ||
"muted", | ||
false, | ||
GObject.ParamFlags.READWRITE, | ||
), | ||
}, | ||
InternalChildren: ["volume_adjustment", "volume_low_button"], | ||
}, | ||
this, | ||
); | ||
|
||
this.set_layout_manager_type(Gtk.BoxLayout.$gtype); | ||
this.set_accessible_role(Gtk.AccessibleRole.GROUP); | ||
|
||
this.install_property_action("volume.toggle-mute", "muted"); | ||
} | ||
|
||
private _volume_adjustment!: Gtk.Adjustment; | ||
private _volume_low_button!: Gtk.Button; | ||
|
||
get volume() { | ||
return this._volume_adjustment.value; | ||
} | ||
|
||
set volume(value: number) { | ||
this._volume_adjustment.value = value; | ||
} | ||
|
||
private prev_volume = 0; | ||
|
||
private _muted = false; | ||
|
||
get muted() { | ||
return this._muted; | ||
} | ||
|
||
set muted(value: boolean) { | ||
if (value === this._muted) return; | ||
|
||
if (value) { | ||
this.prev_volume = this._volume_adjustment.value; | ||
this._volume_adjustment.value = 0; | ||
} else { | ||
this._volume_adjustment.value = this.prev_volume; | ||
} | ||
this._muted = value; | ||
} | ||
|
||
private adjustment_value_changed_cb(adjustment: Gtk.Adjustment) { | ||
this._volume_low_button.icon_name = get_volume_icon_name( | ||
false, | ||
adjustment.value, | ||
); | ||
|
||
this.notify("volume"); | ||
} | ||
|
||
private volume_scale_scrolled_cb( | ||
_: Gtk.EventControllerScroll, | ||
__: number, | ||
dy: number, | ||
) { | ||
const delta = dy * this._volume_adjustment.step_increment; | ||
const d = Math.max( | ||
Math.min( | ||
this._volume_adjustment.value - delta, | ||
this._volume_adjustment.upper, | ||
), | ||
0, | ||
); | ||
this._volume_adjustment.value = d; | ||
|
||
return true; | ||
} | ||
|
||
private listeners = new SignalListeners(); | ||
|
||
vfunc_map() { | ||
super.vfunc_map(); | ||
|
||
this.listeners.add_bindings( | ||
get_player().bind_property( | ||
"cubic-volume", | ||
this, | ||
"volume", | ||
GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.BIDIRECTIONAL, | ||
), | ||
get_player().bind_property( | ||
"muted", | ||
this, | ||
"muted", | ||
GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.BIDIRECTIONAL, | ||
), | ||
); | ||
} | ||
|
||
vfunc_unmap(): void { | ||
this.listeners.clear(); | ||
super.vfunc_unmap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function get_volume_icon_name(muted: boolean, volume: number) { | ||
let icon_name: string; | ||
|
||
if (muted) { | ||
icon_name = "audio-volume-muted-symbolic"; | ||
} else { | ||
if (volume === 0) { | ||
icon_name = "audio-volume-muted-symbolic"; | ||
} else if (volume < 0.33) { | ||
icon_name = "audio-volume-low-symbolic"; | ||
} else if (volume < 0.66) { | ||
icon_name = "audio-volume-medium-symbolic"; | ||
} else { | ||
icon_name = "audio-volume-high-symbolic"; | ||
} | ||
} | ||
|
||
return icon_name; | ||
} |