/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk

« back to all changes in this revision

Viewing changes to src/actionbutton.vala

  • Committer: Gustav Hartvigsson
  • Date: 2025-06-03 20:51:10 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20250603205110-zfozhyqh8werenbv
* Inital release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   This file is part of LoggerOuter.
 
3
 
 
4
 LoggerOuter is free software: you can redistribute it and/or modify it under the
 
5
 terms of the GNU Lesser General Public License as published by the Free Software
 
6
 Foundation, either version 3 of the License, or (at your option) any later
 
7
 version.
 
8
 
 
9
 LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
 
10
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 
12
 License for more details.
 
13
 
 
14
 You should have received a copy of the GNU Lessel General Public License
 
15
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
public class LO.ActionButton : Gtk.Button {
 
19
// This whole class is cursed. :-)
 
20
  Gtk.Image icon;
 
21
  Gtk.Box box;
 
22
  Gtk.Label widget_label;
 
23
 
 
24
  string? exec_action;
 
25
 
 
26
  string _button_text;
 
27
 
 
28
  void run_action () {
 
29
 
 
30
    string[]? argv = null;
 
31
    string[] envp = Environ.get ();
 
32
    Pid pid;
 
33
    try {
 
34
      GLib.Shell.parse_argv (exec_action, out argv);
 
35
 
 
36
      GLib.Process.spawn_async (null,  argv, envp,
 
37
                                GLib.SpawnFlags.DO_NOT_REAP_CHILD | GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP ,
 
38
                                null, out pid);
 
39
      
 
40
    } catch (GLib.SpawnError e) {
 
41
      prensent_dialog ("ERROR: %s\n", e.message);
 
42
    } catch (GLib.ShellError e) {
 
43
      prensent_dialog ("ERROR: %s\n", e.message);
 
44
    }
 
45
    app.try_quit ();
 
46
  }
 
47
 
 
48
 
 
49
  public ActionButton (ActionEntry action) {
 
50
    this.box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
 
51
      homogeneous = false,
 
52
    };
 
53
    
 
54
    if (action.icon != null) {
 
55
      this.icon = new Gtk.Image () {
 
56
        vexpand = false,
 
57
        hexpand = false,
 
58
        pixel_size = 28,
 
59
        width_request = 28,
 
60
        height_request = 28,
 
61
      };
 
62
      string _icon_name;
 
63
      if (!action.icon_is_path) {
 
64
        icon.icon_name = action.icon;
 
65
      } else {
 
66
        _icon_name = strip_prelude_of_path (action.icon);
 
67
        this.icon.set_from_file (_icon_name);
 
68
      }
 
69
      box.append (icon);
 
70
    }
 
71
 
 
72
    if (action.text != null) {
 
73
      _button_text = action.text;
 
74
    } else {
 
75
      _button_text = action.group;
 
76
    }
 
77
 
 
78
    this.exec_action = action.exec;
 
79
 
 
80
    this.widget_label = new Gtk.Label (_button_text) {
 
81
      hexpand = true,
 
82
      vexpand = true,
 
83
      halign = Gtk.Align.CENTER,
 
84
      valign = Gtk.Align.CENTER,
 
85
      margin_end = 28,
 
86
    };
 
87
    box.append (widget_label);
 
88
    this.set_child (this.box);
 
89
 
 
90
    this.clicked.connect (() => {
 
91
      var timer = new LO.Timer (this._button_text, 15, (e) => {
 
92
        if (e == LO.Timer.ExitCode.OK) {
 
93
            this.run_action ();
 
94
        }
 
95
      });
 
96
      timer.set_transient_for (app.get_active_window ());
 
97
      timer.show ();
 
98
    });
 
99
 
 
100
  }
 
101
 
 
102
  construct {
 
103
    this.focusable = true;
 
104
  }
 
105
 
 
106
}