Skip to content

Commit 0e725f1

Browse files
committed
Add fade-in and fade-out animations
1 parent 3628ba0 commit 0e725f1

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

config.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
# Sets the amount of blocks in the progress indicator.
3737
;block-count = 20
3838

39+
# Sets the fade in animation duration in seconds.
40+
;fade-in = 0.2
41+
42+
# Sets the fade out animation duration in seconds.
43+
;fade-out = 0.5
44+
3945
# The color of the notification background in format: rgba([0, 255], [0, 255], [0, 255], [0, 1]).
4046
;background = rgba(160, 160, 160, 0.8)
4147

src/avizo_client.vala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface AvizoService : GLib.Object
1616
public abstract int block_height { owned get; set; }
1717
public abstract int block_spacing { owned get; set; }
1818
public abstract int block_count { owned get; set; }
19+
public abstract double fade_in { owned get; set; }
20+
public abstract double fade_out { owned get; set; }
1921
public abstract Gdk.RGBA background { owned get; set; }
2022
public abstract Gdk.RGBA border_color { owned get; set; }
2123
public abstract Gdk.RGBA bar_fg_color { owned get; set; }
@@ -44,6 +46,8 @@ public class AvizoClient : GLib.Application
4446
private static int _block_height = 10;
4547
private static int _block_spacing = 2;
4648
private static int _block_count = 20;
49+
private static double _fade_in = 0.2;
50+
private static double _fade_out = 0.5;
4751
private static string _background = "";
4852
private static string _border_color = "";
4953
private static string _bar_fg_color = "";
@@ -68,6 +72,8 @@ public class AvizoClient : GLib.Application
6872
{ "block-height", 0, 0, OptionArg.INT, ref _block_height, "Sets the block height of the progress indicator", "INT" },
6973
{ "block-spacing", 0, 0, OptionArg.INT, ref _block_spacing, "Sets the spacing between blocks in the progress indicator", "INT" },
7074
{ "block-count", 0, 0, OptionArg.INT, ref _block_count, "Sets the amount of blocks in the progress indicator", "INT" },
75+
{ "fade-in", 0, 0, OptionArg.DOUBLE, ref _fade_in, "Sets the fade in animation duration in seconds", "DOUBLE" },
76+
{ "fade-out", 0, 0, OptionArg.DOUBLE, ref _fade_out, "Sets the fade out animation duration in seconds", "DOUBLE" },
7177
{ "background", 0, 0, OptionArg.STRING, ref _background, "Sets the color of the notification background in format rgba([0, 255], [0, 255], [0, 255], [0, 1])", "STRING" },
7278
{ "border-color", 0, 0, OptionArg.STRING, ref _border_color, "Sets the color of the notification border in format rgba([0, 255], [0, 255], [0, 255], [0, 1])", "STRING" },
7379
{ "foreground", 0, 0, OptionArg.STRING, ref _bar_fg_color, "Deprecated alias for --bar-fg-color", "STRING" },
@@ -170,6 +176,9 @@ public class AvizoClient : GLib.Application
170176
_service.block_spacing = _block_spacing;
171177
_service.block_count = _block_count;
172178

179+
_service.fade_in = _fade_in;
180+
_service.fade_out = _fade_out;
181+
173182
if (_background != "")
174183
{
175184
var color = parse_rgba(_background);

src/avizo_service.vala

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ public class AvizoWindow : Gtk.Window
8282
public int block_spacing { get; set; }
8383
public int block_count { get; set; }
8484

85+
public double fade_in { get; set; }
86+
public double fade_out { get; set; }
87+
8588
public Gdk.RGBA background { get; set; default = Gdk.RGBA(); }
8689
public Gdk.RGBA border_color { get; set; default = Gdk.RGBA(); }
8790
public Gdk.RGBA bar_fg_color { get; set; default = Gdk.RGBA(); }
8891
public Gdk.RGBA bar_bg_color { get; set; default = Gdk.RGBA(); }
8992

93+
private new double opacity = 0;
94+
private bool is_fade_in = true;
95+
private int64 prev_frame_time = 0;
96+
private uint prev_callback_id = 0;
97+
9098
[GtkChild]
9199
private unowned Gtk.Image image;
92100

@@ -102,6 +110,69 @@ public class AvizoWindow : Gtk.Window
102110
draw.connect(on_draw);
103111
}
104112

113+
public void show_animated()
114+
{
115+
remove_tick_callback(prev_callback_id);
116+
is_fade_in = true;
117+
prev_callback_id = add_tick_callback(animation_tick);
118+
show();
119+
}
120+
121+
public void hide_animated()
122+
{
123+
remove_tick_callback(prev_callback_id);
124+
is_fade_in = false;
125+
prev_callback_id = add_tick_callback(animation_tick);
126+
}
127+
128+
private bool animation_tick(Gtk.Widget widget, Gdk.FrameClock frame_clock)
129+
{
130+
frame_clock.begin_updating();
131+
int64 animation_us_elapsed;
132+
if (prev_frame_time == 0)
133+
{
134+
animation_us_elapsed = 0;
135+
}
136+
else {
137+
138+
animation_us_elapsed = (frame_clock.get_frame_time() - prev_frame_time);
139+
}
140+
prev_frame_time = frame_clock.get_frame_time();
141+
var animation_sec_elapsed = (double)animation_us_elapsed / 1000000;
142+
//var animation_sec_elapsed = (double)us_elapsed(frame_clock) / 1000000;
143+
//print("ms elapsed: %lld, sec elapsed: %f, fade in : %f, fade out: %f\n", animation_us_elapsed / 1000, animation_sec_elapsed, fade_in, fade_out);
144+
if (is_fade_in)
145+
{
146+
if (opacity >= 1)
147+
{
148+
prev_frame_time = 0;
149+
frame_clock.end_updating();
150+
return false;
151+
}
152+
opacity += animation_sec_elapsed/fade_in;
153+
if (opacity > 1) opacity = 1;
154+
print("Fade in: %f\n", opacity);
155+
widget.set_opacity(opacity);
156+
}
157+
else
158+
{
159+
if (opacity <= 0)
160+
{
161+
hide();
162+
prev_frame_time = 0;
163+
frame_clock.end_updating();
164+
return false;
165+
}
166+
opacity -= animation_sec_elapsed/fade_out;
167+
if (opacity < 0) opacity = 0;
168+
print("Fade out: %f\n", opacity);
169+
widget.set_opacity(opacity);
170+
}
171+
172+
frame_clock.end_updating();
173+
return true; // Keep going
174+
}
175+
105176
private bool on_draw(Gtk.Widget widget, Cairo.Context ctx)
106177
{
107178
double block_width = (_width - 2 * padding - 2 * border_width -
@@ -199,7 +270,7 @@ public class AvizoService : GLib.Object
199270
{
200271
private static string[] props = {
201272
"image_path", "image_resource", "image_opacity", "progress", "width", "height", "padding",
202-
"border_radius", "border_width", "block_height", "block_spacing", "block_count", "background", "border_color",
273+
"border_radius", "border_width", "block_height", "block_spacing", "block_count", "fade_in", "fade_out", "background", "border_color",
203274
"bar_fg_color", "bar_bg_color",
204275
};
205276

@@ -216,6 +287,8 @@ public class AvizoService : GLib.Object
216287
public int block_height { get; set; default = 10; }
217288
public int block_spacing { get; set; default = 2; }
218289
public int block_count { get; set; default = 20; }
290+
public double fade_in { get; set; default = 0.2; }
291+
public double fade_out { get; set; default = 0.5; }
219292
public Gdk.RGBA background { get; set; default = rgba(160, 160, 160, 0.8); }
220293
public Gdk.RGBA border_color { get; set; default = rgba(90, 90, 90, 0.8); }
221294
public Gdk.RGBA bar_fg_color { get; set; default = rgba(0, 0, 0, 0.8); }
@@ -253,7 +326,7 @@ public class AvizoService : GLib.Object
253326
if (_open_timeouts == 0)
254327
{
255328
for (int i = 0; i < monitors; i++) {
256-
_windows.index(i).hide();
329+
_windows.index(i).hide_animated();
257330
}
258331
}
259332

@@ -305,7 +378,7 @@ public class AvizoService : GLib.Object
305378
window.set_accept_focus(false);
306379
}
307380

308-
window.show();
381+
window.show_animated();
309382
window.queue_draw();
310383
}
311384
}

0 commit comments

Comments
 (0)