Add config for numbers instead of sliders

Currently doesn't affect the audio flyin.
Implementation for https://github.com/scorpion-26/gBar/issues/35
This commit is contained in:
scorpion-26 2023-08-21 18:19:46 +02:00
parent c2c7395687
commit 1a25dbdec6
7 changed files with 121 additions and 29 deletions

View file

@ -76,6 +76,10 @@ highlight {
color: #ffb86c; color: #ffb86c;
} }
.audio-volume {
font-size: 16px;
color: #ffb86c;
}
.audio-volume trough { .audio-volume trough {
background-color: #44475a; background-color: #44475a;
} }
@ -91,6 +95,10 @@ highlight {
color: #bd93f9; color: #bd93f9;
} }
.mic-volume {
font-size: 16px;
color: #bd93f9;
}
.mic-volume trough { .mic-volume trough {
background-color: #44475a; background-color: #44475a;
} }

View file

@ -126,6 +126,9 @@ highlight {
highlight { highlight {
background-color: $orange; background-color: $orange;
} }
font-size: 16px;
color: $orange;
} }
.mic-icon { .mic-icon {
@ -146,6 +149,9 @@ highlight {
highlight { highlight {
background-color: $purple; background-color: $purple;
} }
font-size: 16px;
color: $purple;
} }
.package-outofdate { .package-outofdate {

View file

@ -69,6 +69,9 @@ AudioRevealer: false
# Sets the rate of change of the slider on each scroll. In Percent # Sets the rate of change of the slider on each scroll. In Percent
AudioScrollSpeed: 5 AudioScrollSpeed: 5
# Display numbers instead of a slider for the two audio widgets. Doesn't affect the audio flyin
AudioNumbers: false
# Command that is run to check if there are out-of-date packages. # Command that is run to check if there are out-of-date packages.
# The script should return *ONLY* a number. If it doesn't output a number, updates are no longer checked. # The script should return *ONLY* a number. If it doesn't output a number, updates are no longer checked.
# Default value is applicable for Arch Linux. (See data/update.sh for a human-readable version) # Default value is applicable for Arch Linux. (See data/update.sh for a human-readable version)

View file

@ -133,6 +133,11 @@ in {
default = 5; default = 5;
description = "Sets the rate of change of the slider on each scroll. In Percent"; description = "Sets the rate of change of the slider on each scroll. In Percent";
}; };
AudioNumbers = mkOption {
type = types.bool;
default = false;
description = "Display numbers instead of a slider for the two audio widgets. Doesn't affect the audio flyin"
};
AudioMinVolume = mkOption { AudioMinVolume = mkOption {
type = types.nullOr types.int; type = types.nullOr types.int;
default = 0; default = 0;

View file

@ -163,6 +163,10 @@ namespace Bar
return TimerResult::Ok; return TimerResult::Ok;
} }
Widget* audioSlider;
Widget* micSlider;
Text* audioIcon;
Text* micIcon;
void OnChangeVolumeSink(Slider&, double value) void OnChangeVolumeSink(Slider&, double value)
{ {
System::SetVolumeSink(value); System::SetVolumeSink(value);
@ -173,14 +177,35 @@ namespace Bar
System::SetVolumeSource(value); System::SetVolumeSource(value);
} }
Slider* audioSlider; // For text
Slider* micSlider; double audioVolume = 0;
Text* audioIcon; void OnChangeVolumeSinkDelta(double delta)
Text* micIcon; {
audioVolume += delta;
audioVolume = std::clamp(audioVolume, 0.0, 1.0);
System::SetVolumeSink(audioVolume);
}
double micVolume = 0;
void OnChangeVolumeSourceDelta(double delta)
{
micVolume += delta;
micVolume = std::clamp(micVolume, 0.0, 1.0);
System::SetVolumeSource(micVolume);
}
TimerResult UpdateAudio(Widget&) TimerResult UpdateAudio(Widget&)
{ {
System::AudioInfo info = System::GetAudioInfo(); System::AudioInfo info = System::GetAudioInfo();
audioSlider->SetValue(info.sinkVolume); if (Config::Get().audioNumbers)
{
audioVolume = info.sinkVolume;
((Text*)audioSlider)->SetText(Utils::ToStringPrecision(info.sinkVolume * 100, "%0.0f") + "%");
}
else
{
((Slider*)audioSlider)->SetValue(info.sinkVolume);
}
if (info.sinkMuted) if (info.sinkMuted)
{ {
audioIcon->SetText("󰝟"); audioIcon->SetText("󰝟");
@ -191,7 +216,15 @@ namespace Bar
} }
if (Config::Get().audioInput) if (Config::Get().audioInput)
{ {
micSlider->SetValue(info.sourceVolume); if (Config::Get().audioNumbers)
{
micVolume = info.sourceVolume;
((Text*)micSlider)->SetText(Utils::ToStringPrecision(info.sourceVolume * 100, "%0.0f") + "%");
}
else
{
((Slider*)micSlider)->SetValue(info.sinkVolume);
}
if (info.sourceMuted) if (info.sourceMuted)
{ {
micIcon->SetText("󰍭"); micIcon->SetText("󰍭");
@ -326,32 +359,66 @@ namespace Bar
Input, Input,
Output Output
}; };
auto widgetAudioSlider = [](Widget& parent, AudioType type) auto widgetAudioVolume = [](Widget& parent, AudioType type)
{ {
auto slider = Widget::Create<Slider>(); if (Config::Get().audioNumbers)
slider->SetOrientation(Utils::GetOrientation());
Utils::SetTransform(*slider, {100, true, Alignment::Fill});
slider->SetInverted(true);
switch (type)
{ {
case AudioType::Input: auto eventBox = Widget::Create<EventBox>();
slider->SetClass("mic-volume"); auto text = Widget::Create<Text>();
slider->OnValueChange(DynCtx::OnChangeVolumeSource); text->SetAngle(Utils::GetAngle());
DynCtx::micSlider = slider.get(); Utils::SetTransform(*text, {-1, true, Alignment::Fill});
break; switch (type)
case AudioType::Output: {
slider->SetClass("audio-volume"); case AudioType::Input:
slider->OnValueChange(DynCtx::OnChangeVolumeSink); text->SetClass("mic-volume");
DynCtx::audioSlider = slider.get(); DynCtx::micSlider = text.get();
break; break;
case AudioType::Output:
text->SetClass("audio-volume");
DynCtx::audioSlider = text.get();
break;
}
eventBox->SetScrollFn(
[type, text = text.get()](EventBox&, ScrollDirection direction)
{
double delta = (double)Config::Get().audioScrollSpeed / 100.;
delta *= direction == ScrollDirection::Down ? -1 : 1;
switch (type)
{
case AudioType::Input: DynCtx::OnChangeVolumeSourceDelta(delta); break;
case AudioType::Output: DynCtx::OnChangeVolumeSinkDelta(delta); break;
}
});
eventBox->AddChild(std::move(text));
parent.AddChild(std::move(eventBox));
} }
slider->SetRange({0, 1, 0.01}); else
slider->SetScrollSpeed((double)Config::Get().audioScrollSpeed / 100.); {
auto slider = Widget::Create<Slider>();
slider->SetOrientation(Utils::GetOrientation());
Utils::SetTransform(*slider, {100, true, Alignment::Fill});
slider->SetInverted(true);
switch (type)
{
case AudioType::Input:
slider->SetClass("mic-volume");
slider->OnValueChange(DynCtx::OnChangeVolumeSource);
DynCtx::micSlider = slider.get();
break;
case AudioType::Output:
slider->SetClass("audio-volume");
slider->OnValueChange(DynCtx::OnChangeVolumeSink);
DynCtx::audioSlider = slider.get();
break;
}
slider->SetRange({0, 1, 0.01});
slider->SetScrollSpeed((double)Config::Get().audioScrollSpeed / 100.);
parent.AddChild(std::move(slider)); parent.AddChild(std::move(slider));
}
}; };
auto widgetAudioBody = [&widgetAudioSlider](Widget& parent, AudioType type) auto widgetAudioBody = [&widgetAudioVolume](Widget& parent, AudioType type)
{ {
auto box = Widget::Create<Box>(); auto box = Widget::Create<Box>();
box->SetSpacing({8, false}); box->SetSpacing({8, false});
@ -387,7 +454,7 @@ namespace Bar
slideRevealer->SetRevealed(hovered); slideRevealer->SetRevealed(hovered);
}); });
{ {
widgetAudioSlider(*revealer, type); widgetAudioVolume(*revealer, type);
} }
box->AddChild(std::move(revealer)); box->AddChild(std::move(revealer));
@ -395,7 +462,7 @@ namespace Bar
else else
{ {
// Straight forward // Straight forward
widgetAudioSlider(*box, type); widgetAudioVolume(*box, type);
} }
box->AddChild(std::move(icon)); box->AddChild(std::move(icon));

View file

@ -187,6 +187,7 @@ void Config::Load()
AddConfigVar("CenterTime", config.centerTime, lineView, foundProperty); AddConfigVar("CenterTime", config.centerTime, lineView, foundProperty);
AddConfigVar("AudioInput", config.audioInput, lineView, foundProperty); AddConfigVar("AudioInput", config.audioInput, lineView, foundProperty);
AddConfigVar("AudioRevealer", config.audioRevealer, lineView, foundProperty); AddConfigVar("AudioRevealer", config.audioRevealer, lineView, foundProperty);
AddConfigVar("AudioNumbers", config.audioNumbers, lineView, foundProperty);
AddConfigVar("NetworkWidget", config.networkWidget, lineView, foundProperty); AddConfigVar("NetworkWidget", config.networkWidget, lineView, foundProperty);
AddConfigVar("WorkspaceScrollOnMonitor", config.workspaceScrollOnMonitor, lineView, foundProperty); AddConfigVar("WorkspaceScrollOnMonitor", config.workspaceScrollOnMonitor, lineView, foundProperty);
AddConfigVar("WorkspaceScrollInvert", config.workspaceScrollInvert, lineView, foundProperty); AddConfigVar("WorkspaceScrollInvert", config.workspaceScrollInvert, lineView, foundProperty);

View file

@ -19,11 +19,13 @@ public:
// Script that returns how many packages are out-of-date. The script should only print a number! // Script that returns how many packages are out-of-date. The script should only print a number!
// See data/update.sh for a human-readable version // See data/update.sh for a human-readable version
std::string checkPackagesCommand = "p=\"$(checkupdates)\"; e=$?; if [ $e -eq 127 ] ; then exit 127; fi; if [ $e -eq 2 ] ; then echo \"0\" && exit 0; fi; echo \"$p\" | wc -l"; std::string checkPackagesCommand =
"p=\"$(checkupdates)\"; e=$?; if [ $e -eq 127 ] ; then exit 127; fi; if [ $e -eq 2 ] ; then echo \"0\" && exit 0; fi; echo \"$p\" | wc -l";
bool centerTime = true; bool centerTime = true;
bool audioRevealer = false; bool audioRevealer = false;
bool audioInput = false; bool audioInput = false;
bool audioNumbers = false; // Affects both audio sliders
bool networkWidget = true; bool networkWidget = true;
bool workspaceScrollOnMonitor = true; // Scroll through workspaces on monitor instead of all bool workspaceScrollOnMonitor = true; // Scroll through workspaces on monitor instead of all
bool workspaceScrollInvert = false; // Up = +1, instead of Up = -1 bool workspaceScrollInvert = false; // Up = +1, instead of Up = -1