/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/timer.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
/*
 
3
   This file is part of LoggerOuter.
 
4
 
 
5
 LoggerOuter is free software: you can redistribute it and/or modify it under the
 
6
 terms of the GNU Lesser General Public License as published by the Free Software
 
7
 Foundation, either version 3 of the License, or (at your option) any later
 
8
 version.
 
9
 
 
10
 LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
 
11
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 
13
 License for more details.
 
14
 
 
15
 You should have received a copy of the GNU Lessel General Public License
 
16
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
public class LO.Timer : Gtk.Window {
 
20
 
 
21
  Gtk.Box layout;
 
22
  Gtk.Box button_box;
 
23
  Gtk.Label timer_label;
 
24
  Gtk.Label text_label;
 
25
  Gtk.Button ok_button;
 
26
  Gtk.Button close_button;
 
27
  
 
28
  private int _time;
 
29
  public int time {
 
30
    construct set {
 
31
      this._time = value;
 
32
    }
 
33
    get {
 
34
      return this._time;
 
35
    }
 
36
  }
 
37
 
 
38
  private string _text;
 
39
  public string text {
 
40
    construct set {
 
41
      this._text = value;
 
42
    }
 
43
    get {
 
44
      return _text;
 
45
    }
 
46
  }
 
47
 
 
48
  public delegate void ReturnCallback (ExitCode exit_code);
 
49
  ReturnCallback callback;
 
50
 
 
51
  construct {
 
52
 
 
53
    this.width_request = 100;
 
54
    this.height_request = 100;
 
55
    this.resizable = false;
 
56
 
 
57
    layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 5) {
 
58
      hexpand = true,
 
59
      vexpand = true,
 
60
      homogeneous = false,
 
61
    };
 
62
    button_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
 
63
      hexpand = true,
 
64
      vexpand = false,
 
65
      homogeneous = true,
 
66
    };
 
67
 
 
68
    timer_label = new Gtk.Label ("Time Remanining: 0") {
 
69
      vexpand = true,
 
70
    };
 
71
 
 
72
    text_label = new Gtk.Label (this.text) {
 
73
      vexpand = true,
 
74
    };
 
75
 
 
76
    layout.append (text_label);
 
77
    layout.append (timer_label);
 
78
 
 
79
    close_button = new Gtk.Button.with_label ("Cancel");
 
80
    close_button.clicked.connect (() => {
 
81
      return_to_caller (ExitCode.CANCEL);
 
82
    });
 
83
    button_box.append (close_button);
 
84
 
 
85
    ok_button = new Gtk.Button.with_label ("OK");
 
86
    ok_button.clicked.connect (() => {
 
87
      return_to_caller (ExitCode.OK);
 
88
    });
 
89
    button_box.append (ok_button);
 
90
 
 
91
    layout.append (button_box);
 
92
    
 
93
    this.child = layout;
 
94
    this.modal = true;
 
95
  }
 
96
 
 
97
  public Timer (string text, int time, owned ReturnCallback callback) {
 
98
    this.text = text;
 
99
    this.time = time;
 
100
    this.callback = (owned) callback;
 
101
 
 
102
    this.text_label.set_text (this.text);
 
103
    this.start_timer ();
 
104
  }
 
105
 
 
106
  public enum ExitCode {
 
107
    INVALID,
 
108
    OK,
 
109
    CANCEL;
 
110
 
 
111
    public string to_string () {
 
112
      switch (this) {
 
113
        case (INVALID):
 
114
          return "INVALID";
 
115
        case (OK):
 
116
          return "OK";
 
117
        case (CANCEL):
 
118
          return "CANCEL";
 
119
        default:
 
120
          assert_not_reached ();
 
121
      }
 
122
    }
 
123
  }
 
124
 
 
125
  private bool label_update () {
 
126
    if (_time == 0) {
 
127
      return_to_caller (ExitCode.OK);
 
128
      return false;
 
129
    }
 
130
    
 
131
    this._time--;
 
132
 
 
133
    string text = @"Time Remanining: $(this._time)";
 
134
    this.timer_label.set_text (text);
 
135
 
 
136
 
 
137
    return true;
 
138
  }
 
139
 
 
140
  private void start_timer () {
 
141
    label_update ();
 
142
    GLib.Timeout.add_seconds (1, label_update);
 
143
  }
 
144
 
 
145
  private void return_to_caller (ExitCode e) {
 
146
      assert (this.callback != null);
 
147
      this.callback (e);
 
148
      this.destroy ();
 
149
  }
 
150
 
 
151
#if TIMER_DEBUG
 
152
 
 
153
  static int main (string[] args) {
 
154
    Gtk.init ();
 
155
    Gtk.Application app = new Gtk.Application ("com.foobar.test", GLib.ApplicationFlags.FLAGS_NONE);
 
156
    app.activate.connect (() => {
 
157
      LO.Timer t = new LO.Timer ("Shutting down", 30, (e) => {
 
158
        print (@"Got: $(e)\n");
 
159
        if (e == LO.Timer.ExitCode.OK) {
 
160
          return 0;
 
161
        } else {
 
162
          return 1;
 
163
        }
 
164
      });
 
165
      t.present ();
 
166
      t.show ();
 
167
      app.add_window (t);
 
168
    });
 
169
 
 
170
    return app.run ();
 
171
  }
 
172
 
 
173
#endif
 
174
 
 
175
}