Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wf-background: allow seamless background updates #200

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions metadata/background.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<_short>Cycle Timeout</_short>
<default>150</default>
</option>
<option name="fade_duration" type="int">
<_short>Fade Duration</_short>
<default>1000</default>
</option>
<option name="randomize" type="bool">
<_short>Randomize</_short>
<default>false</default>
Expand Down
24 changes: 21 additions & 3 deletions src/background/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <gtk-utils.hpp>
#include <gtk-layer-shell.h>
#include <glib-unix.h>

#include "background.hpp"

Expand All @@ -35,6 +36,12 @@ void BackgroundDrawingArea::show_image(Glib::RefPtr<Gdk::Pixbuf> image,

to_image.x = offset_x / this->get_scale_factor();
to_image.y = offset_y / this->get_scale_factor();

fade = {
fade_duration,
wf::animation::smoothing::linear
};

fade.animate(from_image.source ? 0.0 : 1.0, 1.0);

Glib::signal_idle().connect_once([=] ()
Expand Down Expand Up @@ -100,7 +107,7 @@ Glib::RefPtr<Gdk::Pixbuf> WayfireBackground::create_from_file_safe(std::string p
return pbuf;
}

bool WayfireBackground::change_background(int timer)
bool WayfireBackground::change_background()
{
Glib::RefPtr<Gdk::Pixbuf> pbuf;
std::string path;
Expand Down Expand Up @@ -248,8 +255,8 @@ void WayfireBackground::reset_cycle_timeout()
change_bg_conn.disconnect();
if (images.size())
{
change_bg_conn = Glib::signal_timeout().connect(sigc::bind(sigc::mem_fun(
this, &WayfireBackground::change_background), 0), cycle_timeout);
change_bg_conn = Glib::signal_timeout().connect(sigc::mem_fun(
this, &WayfireBackground::change_background), cycle_timeout);
}
}

Expand Down Expand Up @@ -316,6 +323,7 @@ class WayfireBackgroundApp : public WayfireShellApp
{
WayfireShellApp::instance =
std::make_unique<WayfireBackgroundApp>(argc, argv);
g_unix_signal_add(SIGUSR1, sigusr1_handler, (void*)instance.get());
instance->run();
}

Expand All @@ -329,6 +337,16 @@ class WayfireBackgroundApp : public WayfireShellApp
{
backgrounds.erase(output);
}

static gboolean sigusr1_handler(void *instance)
{
for (const auto& [_, bg] : ((WayfireBackgroundApp*)instance)->backgrounds)
{
bg->change_background();
}

return TRUE;
}
};

int main(int argc, char **argv)
Expand Down
9 changes: 4 additions & 5 deletions src/background/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class BackgroundImage

class BackgroundDrawingArea : public Gtk::DrawingArea
{
wf::animation::simple_animation_t fade{
wf::create_option(1000),
wf::animation::smoothing::linear
};
wf::animation::simple_animation_t fade;
WfOption<int> fade_duration{"background/fade_duration"};

/* These two pixbufs are used for fading one background
* image to the next when changing backgrounds or when
* automatically cycling through a directory of images.
Expand Down Expand Up @@ -61,7 +60,6 @@ class WayfireBackground

Glib::RefPtr<Gdk::Pixbuf> create_from_file_safe(std::string path);
bool background_transition_frame(int timer);
bool change_background(int timer);
bool load_images_from_dir(std::string path);
bool load_next_background(Glib::RefPtr<Gdk::Pixbuf> & pbuf, std::string & path);
void reset_background();
Expand All @@ -72,4 +70,5 @@ class WayfireBackground

public:
WayfireBackground(WayfireShellApp *app, WayfireOutput *output);
bool change_background();
};