1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* vala-panel
* Copyright (C) 2015 Konstantin Pugin <ria.freelander@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using ValaPanel;
using Gtk;
public class Clock: Applet
{
private const string TIP_FORMAT = "tooltip-format";
private const string LABEL_FORMAT = "clock-format";
private const string BOLD = "bold-font";
private ToggleButton clock;
private enum Interval
{
AWAITING_FIRST_CHANGE = 0, /* Experimenting to determine interval, waiting for first change */
AWAITING_SECOND_CHANGE = 1, /* Experimenting to determine interval, waiting for second change */
ONE_SECOND = 2, /* Determined that one second interval is necessary */
ONE_MINUTE = 3 /* Determined that one minute interval is sufficient */
}
private Interval exp_interval;
private int exp_count;
private string? prev_clock_val;
private uint timer;
private Window calendar;
internal string clock_format {get; set;}
internal string tooltip_format {get; set;}
internal bool bold_font {get; set;}
public override void constructed()
{
(this.action_group.lookup_action(APPLET_ACTION_CONFIGURE) as SimpleAction).set_enabled(true);
settings.bind(LABEL_FORMAT,this,LABEL_FORMAT,SettingsBindFlags.GET);
settings.bind(TIP_FORMAT,this,TIP_FORMAT,SettingsBindFlags.GET);
settings.bind(BOLD,this,BOLD,SettingsBindFlags.GET);
clock = new ToggleButton();
ValaPanel.setup_button(clock as Button,null,null);
clock.toggled.connect(()=>{
if (clock.get_active())
{
calendar = create_calendar();
calendar.show();
}
else
{
calendar.destroy();
calendar = null;
}
});
this.notify.connect((pspec)=>{
if (pspec.name == BOLD)
style_set_class(clock,get_css(),"-vala-panel-font-weight",false);
else
{
if (timer != 0) Source.remove(timer);
prev_clock_val = null;
exp_count = 0;
exp_interval = Interval.AWAITING_FIRST_CHANGE;
DateTime now = new DateTime.now_local();
timer_set(now);
if (calendar != null)
{
calendar.destroy();
calendar = null;
}
}
});
clock.show();
this.add(clock);
this.show();
}
public override Widget get_settings_ui()
{
string[] names = {
_("Clock Format"),
_("Tooltip Format"),
_("Format codes: man 3 strftime; %n for line break"),
_("Bold font")
};
string[] keys = {
LABEL_FORMAT,
TIP_FORMAT,
null,
BOLD
};
ConfiguratorType[] types = {
ConfiguratorType.STR,
ConfiguratorType.STR,
ConfiguratorType.TRIM,
ConfiguratorType.BOOL,
};
return generic_cfg_widget(settings, names, keys, types);
}
private void popup_position_helper(Gtk.Widget popup,
out int x, out int y)
{
Gtk.Allocation pa;
Gtk.Allocation a;
popup.realize();
popup.get_allocation(out pa);
if (popup.is_toplevel())
{
Gdk.Rectangle ext;
popup.get_window().get_frame_extents(out ext);
pa.width = ext.width;
pa.height = ext.height;
}
if (popup is Gtk.Menu)
{
int min, nat, new_height = 0;
foreach (var item in (popup as Gtk.Menu).get_children())
{
item.get_preferred_height(out min, out nat);
new_height += nat;
}
pa.height = int.max(pa.height,new_height);
}
get_allocation(out a);
get_window().get_origin(out x, out y);
if (!get_has_window())
{
x += a.x;
y += a.y;
}
var edge = edge_from_gravity(toplevel.panel_gravity);
switch (edge)
{
case Gtk.PositionType.TOP:
y+=a.height;
break;
case Gtk.PositionType.BOTTOM:
y-=pa.height;
break;
case Gtk.PositionType.LEFT:
x+=a.width;
break;
case Gtk.PositionType.RIGHT:
x-=pa.width;
break;
}
unowned Gdk.Monitor monitor = get_display().get_monitor_at_point(x,y);
a = (Gtk.Allocation)monitor.get_workarea();
x = x.clamp(a.x,a.x + a.width - pa.width);
y = y.clamp(a.y,a.y + a.height - pa.height);
}
private void set_popup_position(Gtk.Widget popup)
{
int x,y;
popup_position_helper(popup,out x, out y);
if (popup is Gtk.Window)
(popup as Window).move(x,y);
else
popup.get_window().move(x,y);
}
private string get_css()
{
return ".-vala-panel-font-weight{\n"
+ " font-weight: %s;\n".printf(bold_font ? "bold" : "normal")
+ "}";
}
private Window create_calendar()
{
/* Create a new window. */
var win = new Window(WindowType.POPUP);
win.set_default_size(180, 180);
win.set_border_width(5);
/* Create a standard calendar widget as a child of the window. */
var calendar = new Calendar();
var now = new DateTime.now_local();
calendar.set_display_options(CalendarDisplayOptions.SHOW_WEEK_NUMBERS
| CalendarDisplayOptions.SHOW_DAY_NAMES
| CalendarDisplayOptions.SHOW_HEADING);
calendar.mark_day(now.get_day_of_month());
win.add(calendar);
/* Preset the widget position right now to not move it across the screen */
win.set_type_hint(Gdk.WindowTypeHint.UTILITY);
win.set_transient_for(this.toplevel);
win.set_attached_to(this.toplevel);
calendar.show();
set_popup_position(win);
/* Return the widget. */
return win;
}
/* Periodic timer callback.
* Also used during initialization and configuration change to do a redraw. */
private bool update_display()
{
/* Determine the current time. */
var now = new DateTime.now_local();
if (MainContext.current_source().is_destroyed())
return false;
timer_set(now);
/* Determine the content of the clock label and tooltip. */
string label = now.format(clock_format);
string tooltip = now.format(tooltip_format);
/* When we write the clock value, it causes the panel to do a full relayout.
* Since this function may be called too often while the timing experiment is underway,
* we take the trouble to check if the string actually changed first. */
clock.set_label(label);
clock.set_tooltip_text(tooltip);
/* Conduct an experiment to see how often the value changes.
* Use this to decide whether we update the value every second or every minute.
* We need to account for the possibility that the experiment is being run when we cross a minute boundary. */
if (exp_interval < Interval.ONE_SECOND)
{
if (prev_clock_val == null)
/* Initiate the experiment. */
prev_clock_val = label;
else
{
if (prev_clock_val == label)
{
exp_count += 1;
if (exp_count > 3)
{
/* No change within 3 seconds. Assume change no more often than once per minute. */
exp_interval = Interval.ONE_MINUTE;
prev_clock_val = null;
}
}
else if (exp_interval == Interval.AWAITING_FIRST_CHANGE)
{
/* We have a change at the beginning of the experiment, but we do not know when the next change might occur.
* Continue the experiment for 3 more seconds. */
exp_interval = Interval.AWAITING_SECOND_CHANGE;
exp_count = 0;
prev_clock_val = label;
}
else
{
/* We have a second change. End the experiment. */
exp_interval = ((exp_count > 3) ? Interval.ONE_MINUTE : Interval.ONE_SECOND);
prev_clock_val = null;
}
}
}
/* Reset the timer and return. */
return false;
}
/* Set the timer. */
private void timer_set(DateTime current_time)
{
uint microseconds = 1000000;
/* Compute number of microseconds until next second boundary. */
microseconds = 1000000 - (current_time.get_microsecond());
/* If the expiration interval is the minute boundary,
* add number of milliseconds after that until next minute boundary. */
if (exp_interval == Interval.ONE_MINUTE)
{
uint seconds = 60 - current_time.get_second();
microseconds += seconds * 1000000;
}
/* Be defensive, and set the timer. */
if (microseconds <= 0)
microseconds = 1000000;
timer = Timeout.add(microseconds/1000,update_display);
}
} // End class
[ModuleInit]
public void g_io_clock_load(GLib.TypeModule module)
{
// boilerplate - all modules need this
GLib.IOExtensionPoint.implement(ValaPanel.APPLET_EXTENSION_POINT,typeof(Clock),"org.valapanel.clock",10);
}
public void g_io_clock_unload(GLib.IOModule module)
{
// boilerplate - all modules need this
}
|