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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Copyright 2011-2017 Corentin Noël <corentin@elementary.io>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
/**
* An horizontal bar showing the remaining amount of space.
*
* {{../doc/images/StorageBar.png}}
*
* ''Example''<<BR>>
* {{{
* public class StorageView : Gtk.Grid {
* construct {
* var file_root = GLib.File.new_for_path ("/");
*
* try {
* var info = file_root.query_filesystem_info (GLib.FileAttribute.FILESYSTEM_SIZE, null);
*
* var size = info.get_attribute_uint64 (GLib.FileAttribute.FILESYSTEM_SIZE);
*
* var storage = new Granite.Widgets.StorageBar.with_total_usage (size, size/2);
* storage.update_block_size (Granite.Widgets.StorageBar.ItemDescription.AUDIO, size/40);
* storage.update_block_size (Granite.Widgets.StorageBar.ItemDescription.VIDEO, size/30);
* storage.update_block_size (Granite.Widgets.StorageBar.ItemDescription.APP, size/20);
* storage.update_block_size (Granite.Widgets.StorageBar.ItemDescription.PHOTO, size/10);
* storage.update_block_size (Granite.Widgets.StorageBar.ItemDescription.FILES, size/5);
*
* add (storage);
* } catch (Error e) {
* critical (e.message);
* }
* }
* }
* }}}
*/
public class Granite.Widgets.StorageBar : Gtk.Box {
public enum ItemDescription {
OTHER,
AUDIO,
VIDEO,
PHOTO,
APP,
FILES = OTHER;
public static string? get_class (ItemDescription description) {
switch (description) {
case ItemDescription.FILES:
return "files";
case ItemDescription.AUDIO:
return "audio";
case ItemDescription.VIDEO:
return "video";
case ItemDescription.PHOTO:
return "photo";
case ItemDescription.APP:
return "app";
default:
return null;
}
}
public static string get_name (ItemDescription description) {
switch (description) {
case ItemDescription.AUDIO:
return _("Audio");
case ItemDescription.VIDEO:
/// TRANSLATORS: Refers to videos the mime type. Not Videos the app.
return _("Videos");
case ItemDescription.PHOTO:
/// TRANSLATORS: Refers to photos the mime type. Not Photos the app.
return _("Photos");
case ItemDescription.APP:
return _("Apps");
case ItemDescription.FILES:
/// TRANSLATORS: Refers to files the mime type. Not Files the app.
return _("Files");
default:
return _("Other");
}
}
}
private uint64 _storage = 0;
public uint64 storage {
get {
return _storage;
}
set {
_storage = value;
update_size_description ();
}
}
private uint64 _total_usage = 0;
public uint64 total_usage {
get {
return _total_usage;
}
set {
_total_usage = uint64.min (value, storage);
update_size_description ();
}
}
public int inner_margin_sides {
get {
return fillblock_box.margin_start;
}
set {
fillblock_box.margin_end = fillblock_box.margin_start = value;
}
}
private Gtk.Label description_label;
private GLib.HashTable<int, FillBlock> blocks;
private int index = 0;
private Gtk.Box fillblock_box;
private Gtk.Box legend_box;
private FillBlock free_space;
private FillBlock used_space;
/**
* Creates a new StorageBar widget with the given amount of space.
*
* @param storage the total amount of space.
*/
public StorageBar (uint64 storage) {
Object (storage: storage);
}
/**
* Creates a new StorageBar widget with the given amount of space.an a larger total usage block
*
* @param storage the total amount of space.
* @param usage the amount of space used.
*/
public StorageBar.with_total_usage (uint64 storage, uint64 total_usage) {
Object (storage: storage, total_usage: total_usage);
}
static construct {
Granite.init ();
}
construct {
orientation = Gtk.Orientation.VERTICAL;
description_label = new Gtk.Label (null);
description_label.hexpand = true;
description_label.margin_top = 6;
get_style_context ().add_class (Granite.STYLE_CLASS_STORAGEBAR);
blocks = new GLib.HashTable<int, FillBlock> (null, null);
fillblock_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
fillblock_box.get_style_context ().add_class (Gtk.STYLE_CLASS_TROUGH);
fillblock_box.hexpand = true;
inner_margin_sides = 12;
legend_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12);
legend_box.expand = true;
var legend_center_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
legend_center_box.set_center_widget (legend_box);
var legend_scrolled = new Gtk.ScrolledWindow (null, null);
legend_scrolled.vscrollbar_policy = Gtk.PolicyType.NEVER;
legend_scrolled.hexpand = true;
legend_scrolled.add (legend_center_box);
var grid = new Gtk.Grid ();
grid.attach (legend_scrolled, 0, 0, 1, 1);
grid.attach (fillblock_box, 0, 1, 1, 1);
grid.attach (description_label, 0, 2, 1, 1);
set_center_widget (grid);
fillblock_box.size_allocate.connect ((allocation) => {
// lost_size is here because we use truncation so that it is possible for a full device to have a filed bar.
double lost_size = 0;
int current_x = allocation.x;
for (int i = 0; i < blocks.length; i++) {
weak FillBlock block = blocks.get (i);
if (block == null || block.visible == false)
continue;
var new_allocation = Gtk.Allocation ();
new_allocation.x = current_x;
new_allocation.y = allocation.y;
double width = (((double)allocation.width) * (double) block.size / (double) storage) + lost_size;
lost_size -= GLib.Math.trunc (lost_size);
new_allocation.width = (int) GLib.Math.trunc (width);
new_allocation.height = allocation.height;
block.size_allocate_with_baseline (new_allocation, block.get_allocated_baseline ());
lost_size = width - new_allocation.width;
current_x += new_allocation.width;
}
});
create_default_blocks ();
}
private void create_default_blocks () {
var seq = new Sequence<ItemDescription> ();
seq.append (ItemDescription.FILES);
seq.append (ItemDescription.AUDIO);
seq.append (ItemDescription.VIDEO);
seq.append (ItemDescription.PHOTO);
seq.append (ItemDescription.APP);
seq.sort ((a, b) => {
if (a == ItemDescription.FILES)
return 1;
if (b == ItemDescription.FILES)
return -1;
return ItemDescription.get_name (a).collate (ItemDescription.get_name (b));
});
seq.foreach ((description) => {
var fill_block = new FillBlock (description, 0);
fillblock_box.add (fill_block);
legend_box.add (fill_block.legend_item);
blocks.set (index, fill_block);
index++;
});
free_space = new FillBlock (ItemDescription.FILES, storage);
used_space = new FillBlock (ItemDescription.FILES, total_usage);
free_space.get_style_context ().add_class ("empty-block");
free_space.get_style_context ().remove_class ("files");
used_space.get_style_context ().remove_class ("files");
blocks.set (index++, used_space);
blocks.set (index++, free_space);
fillblock_box.add (used_space);
fillblock_box.add (free_space);
update_size_description ();
}
private void update_size_description () {
uint64 user_size = 0;
foreach (weak FillBlock block in blocks.get_values ()) {
if (block.visible == false || block == free_space || block == used_space)
continue;
user_size += block.size;
}
uint64 free;
if (user_size > total_usage) {
free = storage - user_size;
used_space.size = 0;
} else {
free = storage - total_usage;
used_space.size = total_usage - user_size;
}
free_space.size = free;
description_label.label = _("%s free out of %s").printf (GLib.format_size (free), GLib.format_size (storage));
}
/**
* Update the specified block with a given amount of space.
*
* @param description the category to update.
* @param size the size of the category or 0 to hide.
*/
public void update_block_size (ItemDescription description, uint64 size) {
foreach (weak FillBlock block in blocks.get_values ()) {
if (block.description == description) {
block.size = size;
update_size_description ();
return;
}
}
}
internal class FillBlock : FillRound {
private uint64 _size = 0;
public uint64 size {
get {
return _size;
}
set {
_size = value;
if (_size == 0) {
no_show_all = true;
visible = false;
legend_item.no_show_all = true;
legend_item.visible = false;
} else {
no_show_all = false;
visible = true;
legend_item.no_show_all = false;
legend_item.visible = true;
size_label.label = GLib.format_size (_size);
queue_resize ();
}
}
}
public ItemDescription description { public get; construct set; }
public Gtk.Grid legend_item { public get; private set; }
private Gtk.Label name_label;
private Gtk.Label size_label;
private FillRound legend_fill;
internal FillBlock (ItemDescription description, uint64 size) {
Object (size: size, description: description);
var clas = ItemDescription.get_class (description);
if (clas != null) {
get_style_context ().add_class (clas);
legend_fill.get_style_context ().add_class (clas);
}
name_label.label = "<b>%s</b>".printf (GLib.Markup.escape_text (ItemDescription.get_name (description)));
}
construct {
show_all ();
legend_item = new Gtk.Grid ();
legend_item.column_spacing = 6;
name_label = new Gtk.Label (null);
name_label.halign = Gtk.Align.START;
name_label.use_markup = true;
size_label = new Gtk.Label (null);
size_label.halign = Gtk.Align.START;
legend_fill = new FillRound ();
legend_fill.get_style_context ().add_class ("legend");
var legend_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
legend_box.set_center_widget (legend_fill);
legend_item.attach (legend_box, 0, 0, 1, 2);
legend_item.attach (name_label, 1, 0, 1, 1);
legend_item.attach (size_label, 1, 1, 1, 1);
}
}
internal class FillRound : Gtk.Widget {
internal FillRound () {
}
construct {
set_has_window (false);
var style_context = get_style_context ();
style_context.add_class ("fill-block");
expand = true;
}
public override bool draw (Cairo.Context cr) {
var width = get_allocated_width ();
var height = get_allocated_height ();
var context = get_style_context ();
context.render_background (cr, 0, 0, width, height);
context.render_frame (cr, 0, 0, width, height);
return true;
}
public override void get_preferred_width (out int minimum_width, out int natural_width) {
base.get_preferred_width (out minimum_width, out natural_width);
var context = get_style_context ();
var padding = context.get_padding (get_state_flags ());
minimum_width = int.max (padding.left + padding.right, minimum_width);
minimum_width = int.max (1, minimum_width);
natural_width = int.max (minimum_width, natural_width);
}
public override void get_preferred_height (out int minimum_height, out int natural_height) {
base.get_preferred_height (out minimum_height, out natural_height);
var context = get_style_context ();
var padding = context.get_padding (get_state_flags ());
minimum_height = int.max (padding.top + padding.bottom, minimum_height);
minimum_height = int.max (1, minimum_height);
natural_height = int.max (minimum_height, natural_height);
}
}
}
|