statusbar.vala 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class Statusbar : Gtk.Box
  8. {
  9. // Data
  10. public uint _timer_id;
  11. // Widgets
  12. public Gtk.Label _status;
  13. public Gtk.Label _temporary_message;
  14. public Gtk.Button _donate;
  15. public Gtk.Label _version;
  16. public const string IDLE_STATUS = "Idle";
  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. clear_status();
  26. _temporary_message = new Gtk.Label("");
  27. _donate = new Gtk.Button.from_icon_name("hearth-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  28. _donate.can_focus = false;
  29. _donate.get_style_context().add_class("flat");
  30. _donate.clicked.connect(() => {
  31. GLib.Application.get_default().activate_action("donate", null);
  32. });
  33. _version = new Gtk.Label(null);
  34. _version.get_style_context().add_class("colorfast-link");
  35. _version.set_markup("<a href=\"\">" + CROWN_VERSION + "</a>");
  36. _version.get_style_context().add_class("version-label");
  37. _version.can_focus = false;
  38. _version.activate_link.connect(() => {
  39. GLib.Application.get_default().activate_action("changelog", null);
  40. return true;
  41. });
  42. this.pack_start(_status, false, false, 0);
  43. this.pack_start(_temporary_message, false, false, 0);
  44. this.pack_end(_version, false, false, 0);
  45. this.pack_end(_donate, false, false, 6);
  46. this.get_style_context().add_class("statusbar");
  47. }
  48. ~Statusbar()
  49. {
  50. if (_timer_id > 0)
  51. GLib.Source.remove(_timer_id);
  52. }
  53. /// Shows a message on the statusbar and removes it after 4 seconds.
  54. public void set_temporary_message(string message)
  55. {
  56. _temporary_message.set_label("; " + message);
  57. if (_timer_id > 0) {
  58. GLib.Source.remove(_timer_id);
  59. _timer_id = 0;
  60. }
  61. _timer_id = GLib.Timeout.add_seconds(4, () => {
  62. _temporary_message.set_label("");
  63. _timer_id = 0;
  64. return GLib.Source.REMOVE;
  65. });
  66. }
  67. public void set_status(string status)
  68. {
  69. _status.set_text(status);
  70. }
  71. public void clear_status()
  72. {
  73. if (_status == null)
  74. _status = new Gtk.Label(IDLE_STATUS);
  75. else
  76. _status.set_text(IDLE_STATUS);
  77. }
  78. }
  79. } /* namespace Crown */