Skip to content

Commit c793f08

Browse files
jeremypwJeremy Woottenzeebokryonakano
authored
Minor FolderManager code improvements (#1489)
* File: Recognise info is null after construction * File: Provide fallback icon * Simplify is_valid_directory and is_valid_textfile * Fix indentation * Remove extraneous blank line * src/FolderManager/File.vala DRY return _icon Co-authored-by: Ryo Nakano <ryonakaknock3@gmail.com> --------- Co-authored-by: Jeremy Wootten <jeremy@Proteus-EL07R6-9b3c42bb.localdomain> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech> Co-authored-by: Ryo Nakano <ryonakaknock3@gmail.com>
1 parent 104cc77 commit c793f08

3 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/FolderManager/File.vala

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Scratch.FolderManager {
2525
*/
2626
public class File : GLib.Object {
2727
public GLib.File file { get; private set; }
28-
private GLib.FileInfo info;
28+
private GLib.FileInfo? info = null; // Non-null after loading
2929

3030
public File (string path) {
3131
Object (path: path);
@@ -67,7 +67,12 @@ namespace Scratch.FolderManager {
6767
return _icon;
6868
}
6969

70-
_icon = GLib.ContentType.get_icon (info.get_content_type ());
70+
if (info != null) {
71+
_icon = GLib.ContentType.get_icon (info.get_content_type ());
72+
} else {
73+
_icon = new ThemedIcon ("missing-image");
74+
}
75+
7176
return _icon;
7277
}
7378
}
@@ -78,31 +83,29 @@ namespace Scratch.FolderManager {
7883
}
7984

8085
// Checks if we're dealing with a non-backup directory
81-
// Hidden subfolders are not shown by default, but we need to allow hidden top-level folder
82-
public bool is_valid_directory (bool allow_hidden = true) {
83-
if ((!allow_hidden && name.has_prefix (".")) || // If parent is hidden then inherit validity from parent
84-
info.get_is_backup ()) {
85-
86-
return false;
87-
}
86+
// If parent is hidden then inherit validity from parent
87+
private bool? _is_valid_directory = null;
88+
public bool is_valid_directory {
89+
get {
90+
if (_is_valid_directory == null) {
91+
_is_valid_directory = info != null &&
92+
!info.get_is_backup () &&
93+
info.get_file_type () == FileType.DIRECTORY;
94+
}
8895

89-
if (info.get_file_type () == FileType.DIRECTORY) {
90-
return true;
96+
return _is_valid_directory;
9197
}
92-
93-
return false;
9498
}
9599

96-
public bool is_temporary {
97-
get {
98-
return path.has_suffix ("~");
99-
}
100-
}
101-
102100
// checks if we're dealing with a textfile
101+
private bool? _is_valid_textfile = null;
103102
public bool is_valid_textfile {
104103
get {
105-
return Utils.check_if_valid_text_file (path, info);
104+
if (_is_valid_textfile == null) {
105+
_is_valid_textfile = !path.has_suffix ("~") && Utils.check_if_valid_text_file (path, info);
106+
}
107+
108+
return _is_valid_textfile;
106109
}
107110
}
108111

@@ -138,7 +141,7 @@ namespace Scratch.FolderManager {
138141
while ((file_info = enumerator.next_file ()) != null) {
139142
var child = file.get_child (file_info.get_name ());
140143
var child_file = new File (child.get_path ());
141-
if (child_file.is_valid_directory () || child_file.is_valid_textfile) {
144+
if (child_file.is_valid_directory || child_file.is_valid_textfile) {
142145
_children.add (child_file);
143146
}
144147
}
@@ -195,10 +198,10 @@ namespace Scratch.FolderManager {
195198
}
196199

197200
public static int compare (File a, File b) {
198-
if (a.is_valid_directory () && b.is_valid_textfile) {
201+
if (a.is_valid_directory && b.is_valid_textfile) {
199202
return -1;
200203
}
201-
if (a.is_valid_textfile && b.is_valid_directory ()) {
204+
if (a.is_valid_textfile && b.is_valid_directory) {
202205
return 1;
203206
}
204207

src/FolderManager/FileView.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
507507
if (is_open (folder)) {
508508
warning ("Folder '%s' is already open.", folder.path);
509509
return;
510-
} else if (!folder.is_valid_directory (true)) { // Allow hidden top-level folders
510+
} else if (!folder.is_valid_directory) {
511511
warning ("Cannot open invalid directory.");
512512
return;
513513
}

src/FolderManager/FolderItem.vala

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Scratch.FolderManager {
2929
private bool has_dummy;
3030
private Code.Widgets.SourceList.Item dummy; /* Blank item for expanded empty folders */
3131

32-
public FolderItem (File file, FileView view) requires (file.is_valid_directory) {
32+
public FolderItem (File file, FileView view) {
3333
Object (file: file, view: view);
3434
}
3535

@@ -63,16 +63,14 @@ namespace Scratch.FolderManager {
6363
file.children.size > 0) {
6464

6565
foreach (var child in file.children) {
66-
Code.Widgets.SourceList.Item item = null;
67-
if (child.is_valid_directory ()) {
66+
Code.Widgets.SourceList.Item? item = null;
67+
if (child.is_valid_directory) {
6868
item = new FolderItem (child, view);
6969
} else if (child.is_valid_textfile) {
7070
item = new FileItem (child, view);
7171
}
7272

73-
if (item != null) {
74-
add (item);
75-
}
73+
add (item); // ignores null parameter
7674
}
7775

7876
children_loaded = true;
@@ -307,15 +305,13 @@ namespace Scratch.FolderManager {
307305
var path_item = find_item_for_path (source.get_path ());
308306
if (path_item == null) {
309307
var file = new File (source.get_path ());
310-
if (file.is_valid_directory ()) {
308+
if (file.is_valid_directory) {
311309
path_item = new FolderItem (file, view);
312-
} else if (!file.is_temporary) {
310+
} else if (file.is_valid_textfile) {
313311
path_item = new FileItem (file, view);
314-
} else {
315-
break;
316312
}
317313

318-
add (path_item);
314+
add (path_item); // null parameter is silently ignored
319315
}
320316

321317
break;

0 commit comments

Comments
 (0)