Skip to content

Commit 3398289

Browse files
committed
refactor: remove screen functions
These have been up-streamed.
1 parent 447a9d9 commit 3398289

File tree

5 files changed

+40
-271
lines changed

5 files changed

+40
-271
lines changed

include/saucer/modules/desktop.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,13 @@ namespace saucer::modules
5555
desktop(saucer::application *parent);
5656

5757
public:
58-
void open(const std::string &);
58+
[[nodiscard]] std::pair<int, int> mouse_position() const;
5959

6060
public:
6161
template <picker::type Type>
6262
[[nodiscard]] picker::result_t<Type> pick(const picker::options & = {});
6363

6464
public:
65-
[[nodiscard]] std::vector<screen> screens() const;
66-
[[nodiscard]] std::optional<screen> screen_at(const window &) const;
67-
68-
public:
69-
[[nodiscard]] std::pair<int, int> mouse_position() const;
65+
void open(const std::string &);
7066
};
7167
} // namespace saucer::modules

src/cocoa.desktop.mm

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111

1212
namespace saucer::modules
1313
{
14-
void desktop::open(const std::string &uri)
14+
std::pair<int, int> desktop::mouse_position() const
1515
{
1616
const utils::autorelease_guard guard{};
1717

1818
if (!m_parent->thread_safe())
1919
{
20-
return m_parent->dispatch([this, uri] { return open(uri); });
20+
return m_parent->dispatch([this] { return mouse_position(); });
2121
}
2222

23-
auto *const workspace = [NSWorkspace sharedWorkspace];
24-
auto *const str = [NSString stringWithUTF8String:uri.c_str()];
25-
auto *const url = fs::exists(uri) ? [NSURL fileURLWithPath:str] : [NSURL URLWithString:str];
23+
const auto [x, y] = NSEvent.mouseLocation;
2624

27-
[workspace openURL:url];
25+
return {x, y};
2826
}
2927

3028
template <picker::type Type>
@@ -105,69 +103,20 @@
105103
}
106104
}
107105

108-
screen convert(NSScreen *screen)
109-
{
110-
const utils::autorelease_guard guard{};
111-
112-
return {
113-
.id = screen.localizedName.UTF8String,
114-
.size = {screen.frame.size.width, screen.frame.size.height},
115-
.position = {screen.frame.origin.x, screen.frame.origin.y},
116-
};
117-
}
118-
119-
std::vector<screen> desktop::screens() const
120-
{
121-
const utils::autorelease_guard guard{};
122-
123-
if (!m_parent->thread_safe())
124-
{
125-
return m_parent->dispatch([this] { return screens(); });
126-
}
127-
128-
auto *const screens = [NSScreen screens];
129-
130-
std::vector<screen> rtn{};
131-
rtn.reserve(screens.count);
132-
133-
for (NSScreen *entry : screens)
134-
{
135-
rtn.emplace_back(convert(entry));
136-
}
137-
138-
return rtn;
139-
}
140-
141-
std::optional<screen> desktop::screen_at(const window &window) const
106+
void desktop::open(const std::string &uri)
142107
{
143108
const utils::autorelease_guard guard{};
144109

145110
if (!m_parent->thread_safe())
146111
{
147-
return m_parent->dispatch([this, &window] { return screen_at(window); });
148-
}
149-
150-
auto *const screen = window.native<false>()->window.screen;
151-
152-
if (!screen)
153-
{
154-
return std::nullopt;
112+
return m_parent->dispatch([this, uri] { return open(uri); });
155113
}
156114

157-
return convert(screen);
158-
}
159-
160-
std::pair<int, int> desktop::mouse_position() const
161-
{
162-
const utils::autorelease_guard guard{};
163-
164-
if (!m_parent->thread_safe())
165-
{
166-
return m_parent->dispatch([this] { return mouse_position(); });
167-
}
115+
auto *const workspace = [NSWorkspace sharedWorkspace];
116+
auto *const str = [NSString stringWithUTF8String:uri.c_str()];
117+
auto *const url = fs::exists(uri) ? [NSURL fileURLWithPath:str] : [NSURL URLWithString:str];
168118

169-
const auto [x, y] = NSEvent.mouseLocation;
170-
return {x, y};
119+
[workspace openURL:url];
171120
}
172121

173122
INSTANTIATE_PICKER();

src/gtk.desktop.cpp

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,9 @@ namespace saucer::modules
1414
std::make_pair(gtk_file_dialog_save, gtk_file_dialog_save_finish) //
1515
);
1616

17-
void desktop::open(const std::string &uri)
17+
std::pair<int, int> desktop::mouse_position() const // NOLINT(*-static)
1818
{
19-
if (!m_parent->thread_safe())
20-
{
21-
return m_parent->dispatch([this, uri] { return open(uri); });
22-
}
23-
24-
if (!fs::exists(uri))
25-
{
26-
auto launcher = utils::g_object_ptr<GtkUriLauncher>{gtk_uri_launcher_new(uri.c_str())};
27-
gtk_uri_launcher_launch(launcher.get(), nullptr, nullptr, nullptr, nullptr);
28-
return;
29-
}
30-
31-
auto file = utils::g_object_ptr<GFile>{g_file_new_for_path(uri.c_str())};
32-
auto launcher = utils::g_object_ptr<GtkFileLauncher>{gtk_file_launcher_new(file.get())};
33-
34-
return gtk_file_launcher_launch(launcher.get(), nullptr, nullptr, nullptr, nullptr);
19+
return {};
3520
}
3621

3722
fs::path convert(GFile *file)
@@ -121,68 +106,24 @@ namespace saucer::modules
121106
return fut.get();
122107
}
123108

124-
screen convert(GdkMonitor *monitor)
125-
{
126-
const auto *model = gdk_monitor_get_model(monitor);
127-
128-
GdkRectangle rect{};
129-
gdk_monitor_get_geometry(monitor, &rect);
130-
131-
return {
132-
.id = model ? model : "",
133-
.size = {rect.width, rect.height},
134-
.position = {rect.x, rect.y},
135-
};
136-
}
137-
138-
std::vector<screen> desktop::screens() const
139-
{
140-
if (!m_parent->thread_safe())
141-
{
142-
return m_parent->dispatch([this] { return screens(); });
143-
}
144-
145-
auto *const display = gdk_display_get_default();
146-
auto *const monitors = gdk_display_get_monitors(display);
147-
const auto size = g_list_model_get_n_items(monitors);
148-
149-
std::vector<screen> rtn{};
150-
rtn.reserve(size);
151-
152-
for (auto i = 0ul; size > i; ++i)
153-
{
154-
auto *const current = reinterpret_cast<GdkMonitor *>(g_list_model_get_item(monitors, i));
155-
rtn.emplace_back(convert(current));
156-
}
157-
158-
return rtn;
159-
}
160-
161-
std::optional<screen> desktop::screen_at(const window &window) const
109+
void desktop::open(const std::string &uri)
162110
{
163111
if (!m_parent->thread_safe())
164112
{
165-
return m_parent->dispatch([this, &window] { return screen_at(window); });
113+
return m_parent->dispatch([this, uri] { return open(uri); });
166114
}
167115

168-
auto *const display = gdk_display_get_default();
169-
auto *const widget = GTK_WIDGET(window.native<false>()->window.get());
170-
171-
auto *const native = gtk_widget_get_native(widget);
172-
auto *const surface = gtk_native_get_surface(native);
173-
auto *const monitor = gdk_display_get_monitor_at_surface(display, surface);
174-
175-
if (!monitor)
116+
if (!fs::exists(uri))
176117
{
177-
return std::nullopt;
118+
auto launcher = utils::g_object_ptr<GtkUriLauncher>{gtk_uri_launcher_new(uri.c_str())};
119+
gtk_uri_launcher_launch(launcher.get(), nullptr, nullptr, nullptr, nullptr);
120+
return;
178121
}
179122

180-
return convert(monitor);
181-
}
123+
auto file = utils::g_object_ptr<GFile>{g_file_new_for_path(uri.c_str())};
124+
auto launcher = utils::g_object_ptr<GtkFileLauncher>{gtk_file_launcher_new(file.get())};
182125

183-
std::pair<int, int> desktop::mouse_position() const // NOLINT(*-static)
184-
{
185-
return {};
126+
return gtk_file_launcher_launch(launcher.get(), nullptr, nullptr, nullptr, nullptr);
186127
}
187128

188129
INSTANTIATE_PICKER();

src/qt.desktop.cpp

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ namespace saucer::modules
2222
QFileDialog::AnyFile //
2323
);
2424

25-
void desktop::open(const std::string &uri)
25+
std::pair<int, int> desktop::mouse_position() const
2626
{
2727
if (!m_parent->thread_safe())
2828
{
29-
return m_parent->dispatch([this, uri] { return open(uri); });
29+
return m_parent->dispatch([this] { return mouse_position(); });
3030
}
3131

32-
QDesktopServices::openUrl(QString::fromStdString(uri));
32+
const auto pos = QCursor::pos();
33+
34+
return {pos.x(), pos.y()};
3335
}
3436

3537
template <picker::type Type>
@@ -74,64 +76,14 @@ namespace saucer::modules
7476
}
7577
}
7678

77-
screen convert(QScreen *screen)
78-
{
79-
const auto geometry = screen->geometry();
80-
81-
return {
82-
.id = screen->name().toStdString(),
83-
.size = {geometry.width(), geometry.height()},
84-
.position = {geometry.x(), geometry.y()},
85-
};
86-
}
87-
88-
std::vector<screen> desktop::screens() const
89-
{
90-
if (!m_parent->thread_safe())
91-
{
92-
return m_parent->dispatch([this] { return screens(); });
93-
}
94-
95-
const auto &app = m_parent->native<false>()->application;
96-
const auto screens = app->screens();
97-
98-
std::vector<screen> rtn{};
99-
rtn.reserve(screens.size());
100-
101-
for (const auto &entry : screens)
102-
{
103-
rtn.emplace_back(convert(entry));
104-
}
105-
106-
return rtn;
107-
}
108-
109-
std::optional<screen> desktop::screen_at(const window &window) const
110-
{
111-
if (!m_parent->thread_safe())
112-
{
113-
return m_parent->dispatch([this, &window] { return screen_at(window); });
114-
}
115-
116-
auto *const screen = window.native<false>()->window->screen();
117-
118-
if (!screen)
119-
{
120-
return std::nullopt;
121-
}
122-
123-
return convert(screen);
124-
}
125-
126-
std::pair<int, int> desktop::mouse_position() const
79+
void desktop::open(const std::string &uri)
12780
{
12881
if (!m_parent->thread_safe())
12982
{
130-
return m_parent->dispatch([this] { return mouse_position(); });
83+
return m_parent->dispatch([this, uri] { return open(uri); });
13184
}
13285

133-
const auto [x, y] = QCursor::pos();
134-
return {x, y};
86+
QDesktopServices::openUrl(QString::fromStdString(uri));
13587
}
13688

13789
INSTANTIATE_PICKER();

0 commit comments

Comments
 (0)