Skip to content

Commit

Permalink
Merge pull request godotengine#96643 from bruvzg/fs_links
Browse files Browse the repository at this point in the history
[FileSystem Dock] Add symlink indicator and tooltip.
  • Loading branch information
akien-mga committed Sep 12, 2024
2 parents 6b9f441 + da4f5fb commit cc52112
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/classes/TreeItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@
Returns the [Color] modulating the column's icon.
</description>
</method>
<method name="get_icon_overlay" qualifiers="const">
<return type="Texture2D" />
<param index="0" name="column" type="int" />
<description>
Returns the given column's icon overlay [Texture2D].
</description>
</method>
<method name="get_icon_region" qualifiers="const">
<return type="Rect2" />
<param index="0" name="column" type="int" />
Expand Down Expand Up @@ -662,6 +669,14 @@
Modulates the given column's icon with [param modulate].
</description>
</method>
<method name="set_icon_overlay">
<return type="void" />
<param index="0" name="column" type="int" />
<param index="1" name="texture" type="Texture2D" />
<description>
Sets the given cell's icon overlay [Texture2D]. The cell has to be in [constant CELL_MODE_ICON] mode, and icon has to be set. Overlay is drawn on top of icon, in the bottom left corner.
</description>
</method>
<method name="set_icon_region">
<return type="void" />
<param index="0" name="column" type="int" />
Expand Down
9 changes: 9 additions & 0 deletions drivers/unix/dir_access_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ Error DirAccessUnix::remove(String p_path) {
}

p_path = fix_path(p_path);
if (p_path.ends_with("/")) {
p_path = p_path.left(-1);
}

struct stat flags = {};
if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
Expand All @@ -438,6 +441,9 @@ bool DirAccessUnix::is_link(String p_file) {
}

p_file = fix_path(p_file);
if (p_file.ends_with("/")) {
p_file = p_file.left(-1);
}

struct stat flags = {};
if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
Expand All @@ -453,6 +459,9 @@ String DirAccessUnix::read_link(String p_file) {
}

p_file = fix_path(p_file);
if (p_file.ends_with("/")) {
p_file = p_file.left(-1);
}

char buf[256];
memset(buf, 0, 256);
Expand Down
9 changes: 9 additions & 0 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
// Set custom folder color (if applicable).
bool has_custom_color = assigned_folder_colors.has(lpath);
Color custom_color = has_custom_color ? folder_colors[assigned_folder_colors[lpath]] : Color();
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);

if (has_custom_color) {
subdirectory_item->set_icon_modulate(0, editor_is_dark_theme ? custom_color : custom_color * ITEM_COLOR_SCALE);
Expand All @@ -237,6 +238,10 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
subdirectory_item->set_text(0, dname);
subdirectory_item->set_structured_text_bidi_override(0, TextServer::STRUCTURED_TEXT_FILE);
subdirectory_item->set_icon(0, get_editor_theme_icon(SNAME("Folder")));
if (da->is_link(lpath)) {
subdirectory_item->set_icon_overlay(0, get_editor_theme_icon(SNAME("LinkOverlay")));
subdirectory_item->set_tooltip_text(0, vformat(TTR("Link to: %s"), da->read_link(lpath)));
}
subdirectory_item->set_selectable(0, true);
subdirectory_item->set_metadata(0, lpath);
if (!p_select_in_favorites && (current_path == lpath || ((display_mode != DISPLAY_MODE_TREE_ONLY) && current_path.get_base_dir() == lpath))) {
Expand Down Expand Up @@ -309,6 +314,10 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
file_item->set_text(0, fi.name);
file_item->set_structured_text_bidi_override(0, TextServer::STRUCTURED_TEXT_FILE);
file_item->set_icon(0, _get_tree_item_icon(!fi.import_broken, fi.type, fi.icon_path));
if (da->is_link(file_metadata)) {
file_item->set_icon_overlay(0, get_editor_theme_icon(SNAME("LinkOverlay")));
file_item->set_tooltip_text(0, vformat(TTR("Link to: %s"), da->read_link(file_metadata)));
}
file_item->set_icon_max_width(0, icon_size);
Color parent_bg_color = subdirectory_item->get_custom_bg_color(0);
if (has_custom_color) {
Expand Down
1 change: 1 addition & 0 deletions editor/icons/LinkOverlay.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Si

if (icon_region == Rect2i()) {
icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
if (icon_overlay.is_valid()) {
Vector2 offset = icon->get_size() - icon_overlay->get_size();
icon_overlay->draw_rect_region(p_where, Rect2(p_pos + offset, dsize), Rect2(Point2(), icon_overlay->get_size()), p_color);
}
} else {
icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
if (icon_overlay.is_valid()) {
icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
}
}
}

Expand Down Expand Up @@ -477,6 +484,24 @@ Ref<Texture2D> TreeItem::get_icon(int p_column) const {
return cells[p_column].icon;
}

void TreeItem::set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay) {
ERR_FAIL_INDEX(p_column, cells.size());

if (cells[p_column].icon_overlay == p_icon_overlay) {
return;
}

cells.write[p_column].icon_overlay = p_icon_overlay;
cells.write[p_column].cached_minimum_size_dirty = true;

_changed_notify(p_column);
}

Ref<Texture2D> TreeItem::get_icon_overlay(int p_column) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
return cells[p_column].icon_overlay;
}

void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
ERR_FAIL_INDEX(p_column, cells.size());

Expand Down Expand Up @@ -1633,6 +1658,9 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);

ClassDB::bind_method(D_METHOD("set_icon_overlay", "column", "texture"), &TreeItem::set_icon_overlay);
ClassDB::bind_method(D_METHOD("get_icon_overlay", "column"), &TreeItem::get_icon_overlay);

ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);

Expand Down
4 changes: 4 additions & 0 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TreeItem : public Object {
TreeCellMode mode = TreeItem::CELL_MODE_STRING;

Ref<Texture2D> icon;
Ref<Texture2D> icon_overlay;
Rect2i icon_region;
String text;
String xl_text;
Expand Down Expand Up @@ -257,6 +258,9 @@ class TreeItem : public Object {
void set_icon(int p_column, const Ref<Texture2D> &p_icon);
Ref<Texture2D> get_icon(int p_column) const;

void set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay);
Ref<Texture2D> get_icon_overlay(int p_column) const;

void set_icon_region(int p_column, const Rect2 &p_icon_region);
Rect2 get_icon_region(int p_column) const;

Expand Down

0 comments on commit cc52112

Please sign in to comment.