statusbar.vala 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gtk;
  6. namespace Crown
  7. {
  8. public class Statusbar : Gtk.Box
  9. {
  10. // Data
  11. public uint _timer_id;
  12. // Widgets
  13. public Gtk.Label _status;
  14. public Gtk.Label _temporary_message;
  15. public Gtk.Button _donate;
  16. public Gtk.Button _version;
  17. public Statusbar()
  18. {
  19. Object(orientation: Gtk.Orientation.HORIZONTAL, spacing: 0);
  20. this.margin_start = 8;
  21. this.margin_end = 8;
  22. // Data
  23. _timer_id = 0;
  24. // Widgets
  25. _status = new Gtk.Label("Idle");
  26. _temporary_message = new Gtk.Label("");
  27. _donate = new Gtk.Button.from_icon_name("hearth-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  28. _donate.get_style_context().add_class("flat");
  29. _donate.clicked.connect(() => {
  30. GLib.Application.get_default().activate_action("donate", null);
  31. });
  32. _version = new Gtk.Button.with_label(CROWN_VERSION);
  33. _version.get_style_context().add_class("flat");
  34. _version.get_style_context().add_class("compact");
  35. _version.clicked.connect(() => {
  36. GLib.Application.get_default().activate_action("changelog", null);
  37. });
  38. this.pack_start(_status, false, false, 0);
  39. this.pack_start(_temporary_message, false, false, 0);
  40. this.pack_end(_version, false, false, 0);
  41. this.pack_end(_donate, false, false, 6);
  42. this.get_style_context().add_class("statusbar");
  43. }
  44. ~Statusbar()
  45. {
  46. if (_timer_id > 0)
  47. GLib.Source.remove(_timer_id);
  48. }
  49. /// Shows a message on the statusbar and removes it after 4 seconds.
  50. public void set_temporary_message(string message)
  51. {
  52. _temporary_message.set_label("; " + message);
  53. if (_timer_id > 0) {
  54. GLib.Source.remove(_timer_id);
  55. _timer_id = 0;
  56. }
  57. _timer_id = GLib.Timeout.add_seconds(4, () => {
  58. _temporary_message.set_label("");
  59. _timer_id = 0;
  60. return GLib.Source.REMOVE;
  61. });
  62. }
  63. }
  64. } /* namespace Crown */