1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:47:45 +00:00

Taskbar: Support arbitrary *files* as QuickLaunch entries

This commit is contained in:
Maciej 2021-12-29 17:25:44 +01:00 committed by Andreas Kling
parent 07cf2218cb
commit 0252c1f8fa
3 changed files with 46 additions and 7 deletions

View file

@ -6,6 +6,7 @@
#include "QuickLaunchWidget.h" #include "QuickLaunchWidget.h"
#include "LibCore/IODevice.h" #include "LibCore/IODevice.h"
#include "LibDesktop/Launcher.h"
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <LibConfig/Client.h> #include <LibConfig/Client.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
@ -62,6 +63,26 @@ String QuickLaunchEntryExecutable::name() const
return LexicalPath { m_path }.basename(); return LexicalPath { m_path }.basename();
} }
ErrorOr<void> QuickLaunchEntryFile::launch() const
{
if (!Desktop::Launcher::open(URL::create_with_url_or_path(m_path))) {
// FIXME: LaunchServer doesn't inform us about errors
return Error::from_string_literal("Failed to open file");
}
return {};
}
GUI::Icon QuickLaunchEntryFile::icon() const
{
return GUI::FileIconProvider::icon_for_path(m_path);
}
String QuickLaunchEntryFile::name() const
{
// '=' is a special character in config files
return m_path;
}
QuickLaunchWidget::QuickLaunchWidget() QuickLaunchWidget::QuickLaunchWidget()
{ {
set_shrink_to_fit(true); set_shrink_to_fit(true);
@ -99,8 +120,8 @@ OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_config_value(StringView v
if (!value.starts_with("/") && value.ends_with(".af")) { if (!value.starts_with("/") && value.ends_with(".af")) {
auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, value); auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, value);
return make<QuickLaunchEntryAppFile>(Desktop::AppFile::open(af_path)); return make<QuickLaunchEntryAppFile>(Desktop::AppFile::open(af_path));
} else }
return create_from_path(value); return create_from_path(value);
} }
OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_path(StringView path) OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_path(StringView path)
@ -113,10 +134,9 @@ OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_path(StringView path)
return {}; return {};
} }
auto stat = stat_or_error.release_value(); auto stat = stat_or_error.release_value();
if (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) if (S_ISREG(stat.st_mode) && (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
return make<QuickLaunchEntryExecutable>(path); return make<QuickLaunchEntryExecutable>(path);
dbgln("Config value {} is not a valid quick launch entry", path); return make<QuickLaunchEntryFile>(path);
return {};
} }
void QuickLaunchWidget::add_or_adjust_button(String const& button_name, NonnullOwnPtr<QuickLaunchEntry>&& entry) void QuickLaunchWidget::add_or_adjust_button(String const& button_name, NonnullOwnPtr<QuickLaunchEntry>&& entry)
@ -133,8 +153,10 @@ void QuickLaunchWidget::add_or_adjust_button(String const& button_name, NonnullO
button->set_name(button_name); button->set_name(button_name);
button->on_click = [entry = move(entry), this](auto) { button->on_click = [entry = move(entry), this](auto) {
auto result = entry->launch(); auto result = entry->launch();
if (result.is_error()) if (result.is_error()) {
// FIXME: This message box is displayed in a weird position
GUI::MessageBox::show_error(window(), String::formatted("Failed to open quick launch entry: {}", result.release_error())); GUI::MessageBox::show_error(window(), String::formatted("Failed to open quick launch entry: {}", result.release_error()));
}
}; };
button->on_context_menu_request = [this, button_name](auto& context_menu_event) { button->on_context_menu_request = [this, button_name](auto& context_menu_event) {
m_context_menu_app_name = button_name; m_context_menu_app_name = button_name;
@ -170,7 +192,7 @@ void QuickLaunchWidget::drop_event(GUI::DropEvent& event)
for (auto& url : urls) { for (auto& url : urls) {
auto entry = QuickLaunchEntry::create_from_path(url.path()); auto entry = QuickLaunchEntry::create_from_path(url.path());
if (entry) { if (entry) {
auto item_name = entry->name().replace(" ", "", true); auto item_name = entry->name().replace(" ", "", true).replace("=", "", true);
add_or_adjust_button(item_name, entry.release_nonnull()); add_or_adjust_button(item_name, entry.release_nonnull());
Config::write_string("Taskbar", quick_launch, item_name, url.path()); Config::write_string("Taskbar", quick_launch, item_name, url.path());
} }

View file

@ -50,6 +50,20 @@ public:
virtual GUI::Icon icon() const override; virtual GUI::Icon icon() const override;
virtual String name() const override; virtual String name() const override;
private:
String m_path;
};
class QuickLaunchEntryFile : public QuickLaunchEntry {
public:
explicit QuickLaunchEntryFile(String path)
: m_path(move(path))
{
}
virtual ErrorOr<void> launch() const override;
virtual GUI::Icon icon() const override;
virtual String name() const override;
private: private:
String m_path; String m_path;
}; };

View file

@ -17,6 +17,7 @@
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibDesktop/AppFile.h> #include <LibDesktop/AppFile.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/ActionGroup.h> #include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/Menu.h> #include <LibGUI/Menu.h>
@ -47,7 +48,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}); });
TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath unix")); TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath unix"));
GUI::WindowManagerServerConnection::the(); GUI::WindowManagerServerConnection::the();
Desktop::Launcher::ensure_connection();
TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath")); TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath"));