[gnome-usage/tracker-powered-storage-view-wip: 90/103] storage: Colorize rows by file type
- From: Petr Štětka <pstetka src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-usage/tracker-powered-storage-view-wip: 90/103] storage: Colorize rows by file type
- Date: Tue, 2 Apr 2019 12:27:22 +0000 (UTC)
commit 40fdcd2626398d409b8bc5049382180dcf3a5416
Author: Petr Štětka <pstetka redhat com>
Date: Mon Jul 23 13:03:40 2018 +0200
storage: Colorize rows by file type
src/storage/storage-view-item.vala | 39 +++++++++-------
src/storage/storage-view-row.vala | 22 +++++++++
src/storage/storage-view.vala | 93 ++++++++++++++++++++++++++++---------
src/storage/tracker-controller.vala | 3 +-
src/utils.vala | 26 +++++++++++
5 files changed, 142 insertions(+), 41 deletions(-)
---
diff --git a/src/storage/storage-view-item.vala b/src/storage/storage-view-item.vala
index 355c48d..0bfdcd3 100644
--- a/src/storage/storage-view-item.vala
+++ b/src/storage/storage-view-item.vala
@@ -82,24 +82,24 @@ public class Usage.StorageViewItem : GLib.Object {
style_class = "downloads";
break;
}
- } else {
- switch (ontology) {
- case "nmm#MusicPiece":
- style_class = "music";
- break;
- case "nmm#Photo":
- style_class = "pictures";
- break;
- case "nmm#Video":
- style_class = "videos";
- break;
- case "nfo#PaginatedTextDocument":
- case "nfo#PlainTextDocument":
- case "nfo#FileDataObject":
- case "nfo#EBook":
- style_class = "documents";
- break;
- }
+ }
+
+ switch (ontology) {
+ case "nmm#MusicPiece":
+ style_class = "music";
+ break;
+ case "nmm#Photo":
+ style_class = "pictures";
+ break;
+ case "nmm#Video":
+ style_class = "videos";
+ break;
+ case "nfo#PaginatedTextDocument":
+ case "nfo#PlainTextDocument":
+ case "nfo#FileDataObject":
+ case "nfo#EBook":
+ style_class = "documents";
+ break;
}
if(custom_type != null) {
@@ -107,6 +107,9 @@ public class Usage.StorageViewItem : GLib.Object {
case "os":
style_class = "os-tag";
break;
+ case "available-graph":
+ style_class = "available-tag";
+ break;
}
}
diff --git a/src/storage/storage-view-row.vala b/src/storage/storage-view-row.vala
index be0dd94..a58a011 100644
--- a/src/storage/storage-view-row.vala
+++ b/src/storage/storage-view-row.vala
@@ -69,4 +69,26 @@ public class Usage.StorageViewRow : Gtk.ListBoxRow {
if(item.custom_type == "up-folder")
get_style_context().add_class("up-folder");
}
+
+ public void colorize(uint order, uint all_count) {
+ if(order == 0)
+ return;
+
+ var default_color = tag.get_style_context().get_background_color(get_style_context().get_state());
+ var result_color = Utils.generate_color(default_color, order, all_count, true);
+ var css_provider = new Gtk.CssProvider();
+ tag.get_style_context().add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+ var css =
+ @".row-tag {
+ background: $result_color;
+ }";
+
+ try {
+ css_provider.load_from_data(css);
+ }
+ catch (GLib.Error error) {
+ warning("Failed to color StorageViewRow: %s", error.message);
+ }
+ }
}
diff --git a/src/storage/storage-view.vala b/src/storage/storage-view.vala
index a69d6b7..4fa8e13 100644
--- a/src/storage/storage-view.vala
+++ b/src/storage/storage-view.vala
@@ -23,6 +23,8 @@ using GTop;
[GtkTemplate (ui = "/org/gnome/Usage/ui/storage-view.ui")]
public class Usage.NewStorageView : Usage.View {
+ public const uint MIN_PERCENTAGE_SHOWN_FILES = 2;
+
private Sparql.Connection connection;
private TrackerController controller;
private StorageQueryBuilder query_builder;
@@ -44,6 +46,12 @@ public class Usage.NewStorageView : Usage.View {
private StorageViewItem os_item = new StorageViewItem ();
private StorageRowPopover row_popover = new StorageRowPopover();
+ private uint storage_row_i = 0;
+ private uint? shown_rows_number = null;
+
+ private uint64 total_used_size = 0;
+ private uint64 total_free_size = 0;
+ private uint64 total_size = 0;
private UserDirectory[] xdg_folders = {
UserDirectory.DOCUMENTS,
@@ -69,6 +77,7 @@ public class Usage.NewStorageView : Usage.View {
public NewStorageView () {
listbox.row_activated.connect (on_row_activated);
+ graph.min_percentage_shown_files = MIN_PERCENTAGE_SHOWN_FILES;
setup_header_label ();
setup_mount_sizes ();
@@ -79,13 +88,14 @@ public class Usage.NewStorageView : Usage.View {
var storage_row = row as StorageViewRow;
if(storage_row.item.custom_type == "up-folder") {
+ shown_rows_number = null;
+ storage_row_i = 0;
listbox.pop();
+ graph.model = (ListStore) listbox.get_model();
} else if (storage_row.item.type == FileType.DIRECTORY) {
- present_dir.begin (storage_row.item.uri);
+ present_dir.begin (storage_row.item.uri, storage_row.item.dir);
} else if (storage_row.item.custom_type != null) {
row_popover.present(storage_row);
- } else {
- graph.queue_draw ();
}
}
@@ -94,41 +104,64 @@ public class Usage.NewStorageView : Usage.View {
}
private Gtk.Widget create_file_row (Object obj) {
+ var model = listbox.get_model();
var item = obj as StorageViewItem;
-
var row = new StorageViewRow.from_item (item);
row.visible = true;
- if (item.type == FileType.DIRECTORY) {
- controller.get_file_size.begin (item.uri, (obj, res) => {
- try {
- var size = controller.get_file_size.end (res);
- row.size_label.label = Utils.format_size_values (size);
- } catch (GLib.Error error) {
- warning (error.message);
- }
- });
+ if(item.custom_type == "available-graph")
+ return new Gtk.ListBoxRow();
+
+ if(shown_rows_number == null) {
+ shown_rows_number = 0;
+
+ for(int i = 0; i < model.get_n_items(); i++) {
+ if((model.get_item(i) as StorageViewItem).percentage > MIN_PERCENTAGE_SHOWN_FILES)
+ shown_rows_number = shown_rows_number + 1;
+ }
+
+ if(shown_rows_number <= 3) {
+ shown_rows_number = model.get_n_items();
+ }
}
+ if(listbox.get_depth() > 1)
+ row.colorize(storage_row_i, shown_rows_number);
+
+ storage_row_i++;
row.show_all ();
+
return row;
}
- private async void present_dir (string uri) {
+ private async void present_dir (string uri, UserDirectory? dir) {
if (connection == null)
return;
try {
- var model = yield controller.enumerate_children (uri);
+ var model = yield controller.enumerate_children (uri, dir);
var file = File.new_for_uri (uri);
var item = new StorageViewItem.from_file (file);
item.custom_type = "up-folder";
+ item.dir = dir;
+ controller.get_file_size.begin (item.uri, (obj, res) => {
+ try {
+ item.size = controller.get_file_size.end (res);
+ item.percentage = item.size * 100 / (double) total_size;
+ model.insert(0, item);
+
+ shown_rows_number = null;
+ storage_row_i = 0;
+
+ listbox.push (new Gtk.ListBoxRow(), model, create_file_row);
+ graph.model = model;
+ } catch (GLib.Error error) {
+ warning (error.message);
+ }
+ });
- model.insert(0, item);
- listbox.push (new Gtk.ListBoxRow(), model, create_file_row);
- graph.model = model;
} catch (GLib.Error error) {
critical ("Failed to query the store: %s", error.message);
}
@@ -139,8 +172,8 @@ public class Usage.NewStorageView : Usage.View {
}
private void setup_mount_sizes () {
- uint64 total_used_size = 0;
- uint64 total_free_size = 0;
+ total_used_size = 0;
+ total_free_size = 0;
MountList mount_list;
MountEntry[] entries = GTop.get_mountlist (out mount_list, false);
@@ -165,7 +198,9 @@ public class Usage.NewStorageView : Usage.View {
total_free_size += free;
}
- var total_size = total_used_size + total_free_size;
+ os_item.percentage = os_item.size * 100 / (double) total_used_size;
+ total_size = total_used_size + total_free_size;
+
var total_used_percentage = ((double) total_used_size / total_size) * 100;
var total_free_percentage = ((double) total_free_size / total_size) * 100;
@@ -191,10 +226,24 @@ public class Usage.NewStorageView : Usage.View {
var item = new StorageViewItem.from_file (file);
item.dir = dir;
- model.append (item);
+ controller.get_file_size.begin (item.uri, (obj, res) => {
+ try {
+ item.size = controller.get_file_size.end (res);
+ item.percentage = item.size * 100 / (double) total_size;
+ model.insert (1, item);
+ } catch (GLib.Error error) {
+ warning (error.message);
+ }
+ });
}
listbox.push (new Gtk.ListBoxRow(), model, create_file_row);
graph.model = model;
+
+ var available_graph_item = new StorageViewItem ();
+ available_graph_item.size = total_free_size;
+ available_graph_item.custom_type = "available-graph";
+ available_graph_item.percentage = available_graph_item.size * 100 / (double) total_size;
+ graph.model.append(available_graph_item);
}
}
diff --git a/src/storage/tracker-controller.vala b/src/storage/tracker-controller.vala
index 30d4eda..d6e2621 100644
--- a/src/storage/tracker-controller.vala
+++ b/src/storage/tracker-controller.vala
@@ -32,7 +32,7 @@ public class Usage.TrackerController : GLib.Object {
this.connection = connection;
}
- public async GLib.ListStore enumerate_children (string uri) throws GLib.Error {
+ public async GLib.ListStore enumerate_children (string uri, UserDirectory? dir) throws GLib.Error {
var list = new GLib.ListStore (typeof (StorageViewItem));
var query = query_builder.enumerate_children (uri);
@@ -52,6 +52,7 @@ public class Usage.TrackerController : GLib.Object {
var file = File.new_for_uri (n_uri);
var item = new StorageViewItem.from_file (file);
item.ontology = file_type;
+ item.dir = dir;
if (item.type == FileType.DIRECTORY) {
item.size = yield get_file_size (n_uri);
diff --git a/src/utils.vala b/src/utils.vala
index 7a40df0..0a16a1e 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -50,6 +50,32 @@ namespace Usage
return value.to_string() + " B/s";
}
+ public static Gdk.RGBA generate_color(Gdk.RGBA default_color, uint order, uint all_count, bool
reverse = false)
+ {
+ double step = 100 / (double) all_count;
+ uint half_count = all_count / 2;
+
+ if(order >= all_count)
+ order = all_count - 1;
+
+ if(order > (all_count / 2))
+ {
+ double percentage = step * (order - half_count);
+ if(reverse)
+ return Utils.color_lighter(default_color, percentage);
+ else
+ return Utils.color_darker(default_color, percentage);
+ }
+ else
+ {
+ double percentage = step * (half_count - (order-1));
+ if(reverse)
+ return Utils.color_darker(default_color, percentage);
+ else
+ return Utils.color_lighter(default_color, percentage);
+ }
+ }
+
public static Gdk.RGBA color_darker(Gdk.RGBA color, double percentage)
{
color.red = color_field_darker(color.red, percentage);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]