Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "fastrun.hpp" | ||
#include <map> | ||
#include <iostream> | ||
#include <gtkmm/button.h> | ||
#include <glibmm/spawn.h> | ||
|
||
WfFastRunCmd::WfFastRunCmd(command_info cmd) : Gtk::Button() | ||
{ | ||
icon.set_from_icon_name(cmd.icon, Gtk::ICON_SIZE_DND); | ||
label.set_label(cmd.name); | ||
box.add(icon); | ||
box.add(label); | ||
icon.show(); | ||
label.show(); | ||
|
||
add(box); | ||
box.show(); | ||
|
||
// Unfortunately tooltips don't work well in GTK3 popovers | ||
// https://gitlab.gnome.org/GNOME/gtk/issues/1708 | ||
// set_tooltip_text(cmd.cmd); | ||
get_style_context()->add_class("flat"); | ||
signal_clicked().connect_notify([=] { | ||
Glib::spawn_command_line_async(cmd.cmd); | ||
}); | ||
} | ||
|
||
void WayfireFastRun::init(Gtk::HBox *container) | ||
{ | ||
button = std::make_unique<WayfireMenuButton>("panel"); | ||
icon.set_from_icon_name("system-run", Gtk::ICON_SIZE_LARGE_TOOLBAR); | ||
icon.show(); | ||
button->add(icon); | ||
|
||
auto popover = button->get_popover(); | ||
popover->add(button_box); | ||
|
||
container->pack_start(*button, false, false); | ||
|
||
button->show_all(); | ||
|
||
handle_config_reload(); | ||
} | ||
|
||
static bool begins_with(const std::string &str, const std::string &prefix) | ||
{ | ||
return str.substr(0, prefix.size()) == prefix; | ||
} | ||
|
||
void WayfireFastRun::handle_config_reload() | ||
{ | ||
for (auto child : button_box.get_children()) | ||
{ | ||
button_box.remove(*child); | ||
} | ||
|
||
auto section = WayfireShellApp::get().config.get_section("panel"); | ||
const std::string command_prefix = "fastrun_cmd_"; | ||
const std::string icon_prefix = "fastrun_icon_"; | ||
const std::string name_prefix = "fastrun_name_"; | ||
|
||
std::map<std::string, command_info> commands_info; | ||
|
||
for (auto opt : section->get_registered_options()) | ||
{ | ||
if (begins_with(opt->get_name(), command_prefix) && !opt->get_value_str().empty()) | ||
{ | ||
auto cmd_name = opt->get_name().substr(command_prefix.size()); | ||
commands_info.insert({ cmd_name, { opt->get_value_str(), "system-run", opt->get_value_str() }}); | ||
} | ||
if (begins_with(opt->get_name(), icon_prefix)) | ||
{ | ||
auto cmd_name = opt->get_name().substr(icon_prefix.size()); | ||
if (commands_info.count(cmd_name)) | ||
{ | ||
commands_info[cmd_name].icon = opt->get_value_str(); | ||
} | ||
else | ||
{ | ||
std::cout << "There is no fastrun command " << cmd_name << ", the command itself should be declared before its icon.\n"; | ||
} | ||
} | ||
if (begins_with(opt->get_name(), name_prefix)) | ||
{ | ||
auto cmd_name = opt->get_name().substr(name_prefix.size()); | ||
if (commands_info.count(cmd_name)) | ||
{ | ||
commands_info[cmd_name].name = opt->get_value_str(); | ||
} | ||
else | ||
{ | ||
std::cout << "There is no fastrun command " << cmd_name << ", the command itself should be declared before its 'pretty name'.\n"; | ||
} | ||
} | ||
} | ||
|
||
for (const auto &[_, cmd] : commands_info) | ||
{ | ||
auto command = new WfFastRunCmd(cmd); | ||
button_box.add(*command); | ||
} | ||
|
||
button_box.show_all(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef WIDGETS_FASTRUN_HPP | ||
#define WIDGETS_FASTRUN_HPP | ||
|
||
#include "../widget.hpp" | ||
|
||
#include <gtkmm/buttonbox.h> | ||
#include <vector> | ||
#include <gtkmm/image.h> | ||
#include "wf-popover.hpp" | ||
|
||
struct command_info | ||
{ | ||
std::string cmd, icon, name; | ||
}; | ||
|
||
class WfFastRunCmd : public Gtk::Button | ||
{ | ||
Gtk::HBox box; | ||
Gtk::Image icon; | ||
Gtk::Label label; | ||
|
||
public: | ||
WfFastRunCmd(command_info cmd); | ||
}; | ||
|
||
/* Widget which allows to run commands in two clicks. | ||
*/ | ||
class WayfireFastRun : public WayfireWidget | ||
{ | ||
Gtk::Image icon; | ||
std::unique_ptr<WayfireMenuButton> button; | ||
Gtk::VButtonBox button_box; | ||
|
||
public: | ||
void init(Gtk::HBox *container) override; | ||
void handle_config_reload() override; | ||
}; | ||
|
||
#endif /* end of include guard: WIDGETS_CLOCK_HPP */ |