mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:27:45 +00:00
Maps: Add favorites panel with favorite places management
This commit is contained in:
parent
3acbffabf9
commit
5a7f43ad38
10 changed files with 421 additions and 13 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "FavoritesPanel.h"
|
||||
#include "SearchPanel.h"
|
||||
#include "UsersMapWidget.h"
|
||||
#include <LibConfig/Client.h>
|
||||
|
@ -23,11 +24,12 @@ static int constexpr MAP_ZOOM_DEFAULT = 3;
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix proc exec"));
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix proc exec"));
|
||||
|
||||
auto app = TRY(GUI::Application::create(arguments));
|
||||
|
||||
TRY(Core::System::unveil("/bin/MapsSettings", "x"));
|
||||
TRY(Core::System::unveil("/home", "rwc"));
|
||||
TRY(Core::System::unveil("/res", "r"));
|
||||
TRY(Core::System::unveil("/tmp/session/%sid/portal/config", "rw"));
|
||||
TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
|
||||
|
@ -65,11 +67,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
map_widget.set_frame_style(Gfx::FrameStyle::SunkenContainer);
|
||||
map_widget.set_show_users(Config::read_bool("Maps"sv, "MapView"sv, "ShowUsers"sv, false));
|
||||
|
||||
// Panels
|
||||
String init_panel_open_name = TRY(String::from_deprecated_string(Config::read_string("Maps"sv, "Panel"sv, "OpenName"sv, ""sv)));
|
||||
int panel_width = Config::read_i32("Maps"sv, "Panel"sv, "Width"sv, INT_MIN);
|
||||
|
||||
// Search panel
|
||||
auto search_panel = TRY(Maps::SearchPanel::create());
|
||||
search_panel->on_places_change = [&map_widget](auto) { map_widget.remove_markers_with_name("search"sv); };
|
||||
search_panel->on_selected_place_change = [&map_widget](auto const& place) {
|
||||
// Remove old search markers
|
||||
// Remove old search marker
|
||||
map_widget.remove_markers_with_name("search"sv);
|
||||
|
||||
// Add new marker and zoom into it
|
||||
|
@ -77,8 +83,66 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
map_widget.set_center(place.latlng);
|
||||
map_widget.set_zoom(place.zoom);
|
||||
};
|
||||
if (Config::read_bool("Maps"sv, "SearchPanel"sv, "Show"sv, false))
|
||||
main_widget.insert_child_before(search_panel, map_widget);
|
||||
main_widget.insert_child_before(search_panel, map_widget);
|
||||
|
||||
auto show_search_panel = [&]() {
|
||||
if (panel_width != INT_MIN)
|
||||
search_panel->set_preferred_width(panel_width);
|
||||
search_panel->set_visible(true);
|
||||
};
|
||||
auto hide_search_panel = [&](bool save_width = true) {
|
||||
if (save_width)
|
||||
panel_width = search_panel->width();
|
||||
search_panel->set_visible(false);
|
||||
map_widget.remove_markers_with_name("search"sv);
|
||||
search_panel->reset();
|
||||
};
|
||||
if (init_panel_open_name == "search") {
|
||||
show_search_panel();
|
||||
} else {
|
||||
hide_search_panel(false);
|
||||
}
|
||||
|
||||
// Favorites panel
|
||||
auto marker_red_image = TRY(Gfx::Bitmap::load_from_file("/res/graphics/maps/marker-red.png"sv));
|
||||
auto favorites_panel = TRY(Maps::FavoritesPanel::create());
|
||||
favorites_panel->on_favorites_change = [&map_widget, marker_red_image](auto const& favorites) {
|
||||
// Sync all favorites markers
|
||||
map_widget.remove_markers_with_name("favorites"sv);
|
||||
for (auto const& favorite : favorites)
|
||||
map_widget.add_marker({ favorite.latlng, favorite.name, marker_red_image, "favorites"_string });
|
||||
};
|
||||
favorites_panel->on_selected_favorite_change = [&map_widget](auto const& favorite) {
|
||||
// Zoom into favorite marker
|
||||
map_widget.set_center(favorite.latlng);
|
||||
map_widget.set_zoom(favorite.zoom);
|
||||
};
|
||||
favorites_panel->load_favorites();
|
||||
main_widget.insert_child_before(favorites_panel, map_widget);
|
||||
|
||||
auto favorites_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hearts.png"sv));
|
||||
map_widget.add_context_menu_action(GUI::Action::create(
|
||||
"Add to &Favorites", favorites_icon, [favorites_panel, &map_widget](auto&) {
|
||||
MUST(favorites_panel->add_favorite({ "Unnamed place"_string, map_widget.context_menu_latlng(), map_widget.zoom() }));
|
||||
},
|
||||
window));
|
||||
|
||||
auto show_favorites_panel = [&]() {
|
||||
if (panel_width != INT_MIN)
|
||||
favorites_panel->set_preferred_width(panel_width);
|
||||
favorites_panel->set_visible(true);
|
||||
};
|
||||
auto hide_favorites_panel = [&](bool save_width = true) {
|
||||
if (save_width)
|
||||
panel_width = favorites_panel->width();
|
||||
favorites_panel->set_visible(false);
|
||||
favorites_panel->reset();
|
||||
};
|
||||
if (init_panel_open_name == "favorites") {
|
||||
show_favorites_panel();
|
||||
} else {
|
||||
hide_favorites_panel(false);
|
||||
}
|
||||
|
||||
// Main menu actions
|
||||
auto file_menu = window->add_menu("&File"_string);
|
||||
|
@ -90,20 +154,40 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) { GUI::Application::the()->quit(); }));
|
||||
|
||||
auto view_menu = window->add_menu("&View"_string);
|
||||
|
||||
RefPtr<GUI::Action> show_favorites_panel_action;
|
||||
auto show_search_panel_action = GUI::Action::create_checkable(
|
||||
"Show search panel", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"sv)), [&main_widget, search_panel, &map_widget](auto& action) {
|
||||
"Show &search panel", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"sv)), [&](auto& action) {
|
||||
if (favorites_panel->is_visible()) {
|
||||
show_favorites_panel_action->set_checked(false);
|
||||
hide_favorites_panel();
|
||||
}
|
||||
if (action.is_checked()) {
|
||||
main_widget.insert_child_before(search_panel, map_widget);
|
||||
show_search_panel();
|
||||
} else {
|
||||
map_widget.remove_markers_with_name("search"sv);
|
||||
search_panel->reset();
|
||||
main_widget.remove_child(search_panel);
|
||||
hide_search_panel();
|
||||
}
|
||||
},
|
||||
window);
|
||||
show_search_panel_action->set_checked(Config::read_bool("Maps"sv, "SearchPanel"sv, "Show"sv, false));
|
||||
show_search_panel_action->set_checked(search_panel->is_visible());
|
||||
|
||||
show_favorites_panel_action = GUI::Action::create_checkable(
|
||||
"Show &favorites panel", favorites_icon, [&](auto& action) {
|
||||
if (search_panel->is_visible()) {
|
||||
show_search_panel_action->set_checked(false);
|
||||
hide_search_panel();
|
||||
}
|
||||
if (action.is_checked()) {
|
||||
show_favorites_panel();
|
||||
} else {
|
||||
hide_favorites_panel();
|
||||
}
|
||||
},
|
||||
window);
|
||||
show_favorites_panel_action->set_checked(favorites_panel->is_visible());
|
||||
|
||||
auto show_users_action = GUI::Action::create_checkable(
|
||||
"Show SerenityOS users", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/ladyball.png"sv)), [&map_widget](auto& action) { map_widget.set_show_users(action.is_checked()); }, window);
|
||||
"Show SerenityOS &users", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/ladyball.png"sv)), [&map_widget](auto& action) { map_widget.set_show_users(action.is_checked()); }, window);
|
||||
show_users_action->set_checked(map_widget.show_users());
|
||||
auto zoom_in_action = GUI::CommonActions::make_zoom_in_action([&map_widget](auto&) { map_widget.set_zoom(map_widget.zoom() + 1); }, window);
|
||||
auto zoom_out_action = GUI::CommonActions::make_zoom_out_action([&map_widget](auto&) { map_widget.set_zoom(map_widget.zoom() - 1); }, window);
|
||||
|
@ -115,6 +199,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
},
|
||||
window);
|
||||
view_menu->add_action(show_search_panel_action);
|
||||
view_menu->add_action(adopt_ref(*show_favorites_panel_action));
|
||||
view_menu->add_separator();
|
||||
view_menu->add_action(show_users_action);
|
||||
view_menu->add_separator();
|
||||
|
@ -133,6 +218,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
// Main toolbar actions
|
||||
toolbar.add_action(show_search_panel_action);
|
||||
toolbar.add_action(adopt_ref(*show_favorites_panel_action));
|
||||
toolbar.add_separator();
|
||||
toolbar.add_action(show_users_action);
|
||||
toolbar.add_separator();
|
||||
|
@ -146,7 +232,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
// Remember last window state
|
||||
int exec = app->exec();
|
||||
Config::write_bool("Maps"sv, "SearchPanel"sv, "Show"sv, show_search_panel_action->is_checked());
|
||||
|
||||
if (search_panel->is_visible()) {
|
||||
Config::write_string("Maps"sv, "Panel"sv, "OpenName"sv, "search"sv);
|
||||
Config::write_i32("Maps"sv, "Panel"sv, "Width"sv, search_panel->width());
|
||||
} else if (favorites_panel->is_visible()) {
|
||||
Config::write_string("Maps"sv, "Panel"sv, "OpenName"sv, "favorites"sv);
|
||||
Config::write_i32("Maps"sv, "Panel"sv, "Width"sv, favorites_panel->width());
|
||||
} else {
|
||||
Config::remove_key("Maps"sv, "Panel"sv, "OpenName"sv);
|
||||
Config::remove_key("Maps"sv, "Panel"sv, "Width"sv);
|
||||
}
|
||||
|
||||
Config::write_string("Maps"sv, "MapView"sv, "CenterLatitude"sv, TRY(String::number(map_widget.center().latitude)));
|
||||
Config::write_string("Maps"sv, "MapView"sv, "CenterLongitude"sv, TRY(String::number(map_widget.center().longitude)));
|
||||
Config::write_i32("Maps"sv, "MapView"sv, "Zoom"sv, map_widget.zoom());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue