| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* @class |
| 20 |
* Folder Arragement Interface |
| 21 |
*/ |
| 22 |
public interface DesktopFolder.FolderArrangement : Object { |
| 23 |
public static int DEFAULT_PADDING = 10; |
| 24 |
public static int DEFAULT_EXTERNAL_MARGIN = 10; |
| 25 |
public static int ARRANGEMENT_TYPE_FREE = 1; |
| 26 |
public static int ARRANGEMENT_TYPE_GRID = 2; |
| 27 |
public static int ARRANGEMENT_TYPE_MANAGED = 3; |
| 28 |
|
| 29 |
/** |
| 30 |
* @name have_margin |
| 31 |
* @description indicates whether the arrangment have internal margins at the panel |
| 32 |
* to put items inside the panel |
| 33 |
* @return bool true->yes we have margins, false otherwise |
| 34 |
*/ |
| 35 |
public abstract bool have_margin (); |
| 36 |
|
| 37 |
/** |
| 38 |
* @name can_drag |
| 39 |
* @description return whether the items dan be dragged or not |
| 40 |
* @return bool true->yes they can be dragged, false otherwise |
| 41 |
*/ |
| 42 |
public abstract bool can_drag (); |
| 43 |
|
| 44 |
/** |
| 45 |
* @name get_sensitivity |
| 46 |
* @description Get the value of sensitivity, used to calculate the alignment of the items |
| 47 |
*/ |
| 48 |
public abstract int get_sensitivity (); |
| 49 |
|
| 50 |
/** |
| 51 |
* @name can_organize |
| 52 |
* @description return whether the arrangement allow the reorganization of items, asked manually |
| 53 |
* @return bool true->yes, it is allowed, false otherwise |
| 54 |
*/ |
| 55 |
public abstract bool can_organize (); |
| 56 |
|
| 57 |
/** |
| 58 |
* @name force_organization |
| 59 |
* @description indicates whether the arrangement must force the organization when the panel is resized |
| 60 |
* @return bool true->yes, force, othwerise false |
| 61 |
*/ |
| 62 |
public abstract bool force_organization (); |
| 63 |
|
| 64 |
/** |
| 65 |
* @name start_drag |
| 66 |
* @description a itemview has started being dragged into the arrangement |
| 67 |
* @param {ItemView} view the view that is being dragged |
| 68 |
*/ |
| 69 |
public abstract void start_drag (ItemView view); |
| 70 |
|
| 71 |
/** |
| 72 |
* @name motion_drag |
| 73 |
* @description a itemview is being moved (the started was notified previously) |
| 74 |
* @param {int} x the x new position |
| 75 |
* @param {int} y the y new position |
| 76 |
*/ |
| 77 |
public abstract void motion_drag (int x, int y); |
| 78 |
|
| 79 |
/** |
| 80 |
* @name end_drag |
| 81 |
* @description the item drag was finished |
| 82 |
*/ |
| 83 |
public abstract void end_drag (); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Factory method to obtain an arragement type |
| 89 |
* @see ARRAGEMENT_TYPE_FREE, ARRAGEMENT_TYPE_GRID, ARRAGEMENT_TYPE_MANAGED constants |
| 90 |
* @param int type the factory type |
| 91 |
* @return FolderArrangement the concrete folder arragement for that type, null if none valid |
| 92 |
*/ |
| 93 |
public static FolderArrangement ? factory (int type) { |
| 94 |
if (type == FolderArrangement.ARRANGEMENT_TYPE_FREE) { |
| 95 |
return new FolderArrangementFree (); |
| 96 |
} else if (type == FolderArrangement.ARRANGEMENT_TYPE_GRID) { |
| 97 |
return new FolderArrangementGrid (); |
| 98 |
} else if (type == FolderArrangement.ARRANGEMENT_TYPE_MANAGED) { |
| 99 |
return new FolderArrangementManaged (); |
| 100 |
} |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @name organize_items |
| 106 |
* @desription organize the list of items in a panel sorted |
| 107 |
* @param parent_window FolderWindow the parent panel of the items |
| 108 |
* @param items List<ItemManager> the reference for the list of items to organize. Items are positioned to foce the new organization |
| 109 |
* @param sort_by_type int the sort type @see FolderSort constants |
| 110 |
* @param asc bool to indicate ascendent sort or descent (true->ascendent) |
| 111 |
*/ |
| 112 |
public static void organize_items (FolderWindow parent_window, ref List <ItemManager> items, int sort_by_type, bool asc, bool vertically) { |
| 113 |
Organize.Thread fot = new Organize.Thread (parent_window, ref items, sort_by_type, asc, vertically); |
| 114 |
fot.start (); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Class to represent a desired movement of an item to a different positions after a reorganization |
| 121 |
*/ |
| 122 |
private class DesktopFolder.Organize.ItemMove { |
| 123 |
/** the view to move */ |
| 124 |
public ItemView view { get; set; } |
| 125 |
/** the new point x,y to put the item view*/ |
| 126 |
public Gdk.Point point { get; set; } |
| 127 |
|
| 128 |
public ItemMove (ItemView view, Gdk.Point point) { |
| 129 |
this.view = view; |
| 130 |
this.point = point; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Thread to organize items following a grid |
| 136 |
*/ |
| 137 |
private class DesktopFolder.Organize.Thread { |
| 138 |
private FolderWindow parent_window; |
| 139 |
private List <ItemManager> items; |
| 140 |
private int sort_by_type; |
| 141 |
private bool asc; |
| 142 |
private bool vertically; |
| 143 |
|
| 144 |
public Thread (FolderWindow parent_window, ref List <ItemManager> items, int sort_by_type, bool asc, bool vertically) { |
| 145 |
this.parent_window = parent_window; |
| 146 |
|
| 147 |
// HELP! how to copy a ref variable in vala!? |
| 148 |
this.items = new List <ItemManager>(); |
| 149 |
items.foreach ((i) => { |
| 150 |
this.items.append (i); |
| 151 |
}) ; |
| 152 |
// ****************************************** |
| 153 |
|
| 154 |
this.asc = asc; |
| 155 |
this.sort_by_type = sort_by_type; |
| 156 |
this.vertically = vertically; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @name start |
| 161 |
* @description start the thread |
| 162 |
*/ |
| 163 |
public void start () { |
| 164 |
parent_window.show_loading (); |
| 165 |
try { |
| 166 |
new GLib.Thread <bool> .try ("sync_thread", this.organize); |
| 167 |
} catch (Error e) { |
| 168 |
stderr.printf ("Error: %s\n", e.message); |
| 169 |
Util.show_error_dialog ("Error", e.message); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @name organize |
| 175 |
* @description organize algorithm |
| 176 |
*/ |
| 177 |
private bool organize () { |
| 178 |
debug ("ORGANIZE THREAD -> INIT"); |
| 179 |
FolderSort folder_sort = FolderSort.factory (sort_by_type); |
| 180 |
folder_sort.sort (ref items, asc); |
| 181 |
|
| 182 |
// window width |
| 183 |
int width = parent_window.get_manager ().get_settings ().w; |
| 184 |
int height = parent_window.get_manager ().get_settings ().h; |
| 185 |
// left margin to start the grid |
| 186 |
int left_margin = FolderArrangement.DEFAULT_EXTERNAL_MARGIN; |
| 187 |
|
| 188 |
// cursors pixel |
| 189 |
int cursor_x = left_margin; |
| 190 |
int cursor_y = 0; |
| 191 |
int padding = parent_window.get_manager ().get_settings ().arrangement_padding; |
| 192 |
Gee.HashMap <string, ItemSettings> managed_items = parent_window.get_manager ().get_settings ().get_items_parsed (); |
| 193 |
Gee.List <Organize.ItemMove> item_moves = new Gee.ArrayList <Organize.ItemMove>(); |
| 194 |
|
| 195 |
for (int i = 0; i < items.length (); i++) { |
| 196 |
ItemManager item = items.nth_data (i); |
| 197 |
|
| 198 |
// moving in the view to the correct position |
| 199 |
// parent_window.move_item (item.get_view (), cursor_x, cursor_y); |
| 200 |
Gdk.Point px = Gdk.Point (); |
| 201 |
px.x = cursor_x; |
| 202 |
px.y = cursor_y; |
| 203 |
|
| 204 |
// add the movement to the pending movements to be executed in the gtk thread |
| 205 |
item_moves.add (new Organize.ItemMove (item.get_view (), px)); |
| 206 |
|
| 207 |
// saving settings for the new position |
| 208 |
ItemSettings is = managed_items[item.get_file_name ()]; |
| 209 |
|
| 210 |
is.x = cursor_x; |
| 211 |
is.y = cursor_y; |
| 212 |
|
| 213 |
// moving the cursor horizontally |
| 214 |
if (vertically) { |
| 215 |
cursor_y = cursor_y + DesktopFolder.ICON_DEFAULT_WIDTH + padding; |
| 216 |
if (cursor_y + DesktopFolder.ICON_DEFAULT_WIDTH > height) { |
| 217 |
// we need to move to the next rows |
| 218 |
cursor_x = cursor_x + DesktopFolder.ICON_DEFAULT_WIDTH + padding; |
| 219 |
cursor_y = 0; |
| 220 |
} |
| 221 |
} else { |
| 222 |
cursor_x = cursor_x + DesktopFolder.ICON_DEFAULT_WIDTH + padding; |
| 223 |
if (cursor_x + DesktopFolder.ICON_DEFAULT_WIDTH + left_margin > width) { |
| 224 |
// we need to move to the next rows |
| 225 |
cursor_x = left_margin; |
| 226 |
cursor_y = cursor_y + DesktopFolder.ICON_DEFAULT_WIDTH + padding; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
// only last to put the items in their positions, this is something can only be done in the gtk main thread |
| 233 |
GLib.Idle.add_full (GLib.Priority.LOW, () => { |
| 234 |
const int MAX = 5; |
| 235 |
int cursor = 0; |
| 236 |
|
| 237 |
while (cursor < MAX && item_moves.size > 0) { |
| 238 |
Organize.ItemMove move = item_moves.remove_at (0); |
| 239 |
UtilGtkAnimation.animate_move (move.view, move.point, 500, UtilFx.AnimationMode.EASE_IN_BACK); |
| 240 |
cursor++; |
| 241 |
} |
| 242 |
|
| 243 |
if (item_moves.size > 0) { |
| 244 |
// next iteration in the main gtk thread |
| 245 |
return true; |
| 246 |
} else { |
| 247 |
parent_window.get_manager ().get_settings ().serialize_array (managed_items.values.to_array ()); |
| 248 |
parent_window.get_manager ().get_settings ().save (); |
| 249 |
|
| 250 |
this.parent_window.hide_loading (); |
| 251 |
debug ("ORGANIZE THREAD -> END"); |
| 252 |
return false; |
| 253 |
} |
| 254 |
}); |
| 255 |
|
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
private class GridRow { |
| 263 |
|
| 264 |
} |