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;
}
.audio-volume {
font-size: 16px;
color: #ffb86c;
}
.audio-volume trough {
background-color: #44475a;
}
@ -91,6 +95,10 @@ highlight {
color: #bd93f9;
}
.mic-volume {
font-size: 16px;
color: #bd93f9;
}
.mic-volume trough {
background-color: #44475a;
}

View file

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

View file

@ -69,6 +69,9 @@ AudioRevealer: false
# Sets the rate of change of the slider on each scroll. In Percent
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.
# 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)

View file

@ -133,6 +133,11 @@ in {
default = 5;
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 {
type = types.nullOr types.int;
default = 0;

View file

@ -163,6 +163,10 @@ namespace Bar
return TimerResult::Ok;
}
Widget* audioSlider;
Widget* micSlider;
Text* audioIcon;
Text* micIcon;
void OnChangeVolumeSink(Slider&, double value)
{
System::SetVolumeSink(value);
@ -173,14 +177,35 @@ namespace Bar
System::SetVolumeSource(value);
}
Slider* audioSlider;
Slider* micSlider;
Text* audioIcon;
Text* micIcon;
// For text
double audioVolume = 0;
void OnChangeVolumeSinkDelta(double delta)
{
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&)
{
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)
{
audioIcon->SetText("󰝟");
@ -191,7 +216,15 @@ namespace Bar
}
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)
{
micIcon->SetText("󰍭");
@ -326,32 +359,66 @@ namespace Bar
Input,
Output
};
auto widgetAudioSlider = [](Widget& parent, AudioType type)
auto widgetAudioVolume = [](Widget& parent, AudioType type)
{
auto slider = Widget::Create<Slider>();
slider->SetOrientation(Utils::GetOrientation());
Utils::SetTransform(*slider, {100, true, Alignment::Fill});
slider->SetInverted(true);
switch (type)
if (Config::Get().audioNumbers)
{
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;
auto eventBox = Widget::Create<EventBox>();
auto text = Widget::Create<Text>();
text->SetAngle(Utils::GetAngle());
Utils::SetTransform(*text, {-1, true, Alignment::Fill});
switch (type)
{
case AudioType::Input:
text->SetClass("mic-volume");
DynCtx::micSlider = text.get();
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});
slider->SetScrollSpeed((double)Config::Get().audioScrollSpeed / 100.);
else
{
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>();
box->SetSpacing({8, false});
@ -387,7 +454,7 @@ namespace Bar
slideRevealer->SetRevealed(hovered);
});
{
widgetAudioSlider(*revealer, type);
widgetAudioVolume(*revealer, type);
}
box->AddChild(std::move(revealer));
@ -395,7 +462,7 @@ namespace Bar
else
{
// Straight forward
widgetAudioSlider(*box, type);
widgetAudioVolume(*box, type);
}
box->AddChild(std::move(icon));

View file

@ -187,6 +187,7 @@ void Config::Load()
AddConfigVar("CenterTime", config.centerTime, lineView, foundProperty);
AddConfigVar("AudioInput", config.audioInput, lineView, foundProperty);
AddConfigVar("AudioRevealer", config.audioRevealer, lineView, foundProperty);
AddConfigVar("AudioNumbers", config.audioNumbers, lineView, foundProperty);
AddConfigVar("NetworkWidget", config.networkWidget, lineView, foundProperty);
AddConfigVar("WorkspaceScrollOnMonitor", config.workspaceScrollOnMonitor, 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!
// 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 audioRevealer = false;
bool audioInput = false;
bool audioNumbers = false; // Affects both audio sliders
bool networkWidget = true;
bool workspaceScrollOnMonitor = true; // Scroll through workspaces on monitor instead of all
bool workspaceScrollInvert = false; // Up = +1, instead of Up = -1