Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make debug windows right edge sticky #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions browser/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <imgui-SFML.h>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <imgui_internal.h>
#include <spdlog/spdlog.h>

#include <algorithm>
Expand Down Expand Up @@ -134,6 +135,8 @@ App::App(std::string browser_title, std::string start_page_hint, bool load_start
engine_.set_on_page_loaded(std::bind_front(&App::on_page_loaded, this));
engine_.set_on_layout_updated(std::bind_front(&App::on_layout_updated, this));

prev_sticky_w = 0;

if (load_start_page) {
ensure_has_scheme(url_buf_);
navigate();
Expand All @@ -159,6 +162,20 @@ void App::set_scale(unsigned scale) {
engine_.set_layout_width(windowSize.x / scale_);
}

void App::sticky_windows(const sf::Event::SizeEvent &size) {
int delta = prev_sticky_w - size.width;
for (ImGuiWindow *win : GImGui->Windows) {
if (!win) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a programming error if it ever happens? Until we have a saner way of keeping the names in sync, just assert() that we have the window we're looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a programming error if it ever happens?

No, because we might decide that windows are ephemeral. I can think of plenty of reasons we might want to destroy some # of them at runtime.

Assert will crash the app, window stickiness should be a "best effort" feature. No reason to make the app user hostile when it doesn't need to be.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, all windows always exist. If that changes in the future it will be because a developer changes it and they probably need to have a look at this code and think about what they're doing.

Since you made me look at the imgui internals now, did you have a look at using GImGui.Windows for this so we don't have to hard-code a list of window names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes/no?

continue;
}
int edge = static_cast<int>(win->Pos.x) + static_cast<int>(win->Size.x);
if (edge == prev_sticky_w) {
ImGui::SetWindowPos(win, {win->Pos.x - delta, win->Pos.y});
}
}
prev_sticky_w = size.width;
}

int App::run() {
while (window_.isOpen()) {
sf::Event event;
Expand All @@ -173,6 +190,7 @@ int App::run() {
case sf::Event::Resized: {
canvas_->set_viewport_size(event.size.width, event.size.height);
engine_.set_layout_width(event.size.width / scale_);
sticky_windows(event.size);
break;
}
case sf::Event::KeyPressed: {
Expand Down
3 changes: 3 additions & 0 deletions browser/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Cursor.hpp>
#include <SFML/Window/Event.hpp>

#include <memory>
#include <string>
Expand Down Expand Up @@ -74,6 +75,8 @@ class App final {

void navigate();
void layout();
void sticky_windows(const sf::Event::SizeEvent &);
int prev_sticky_w;

std::vector<dom::Node const *> get_hovered_nodes(geom::Position document_position) const;
geom::Position to_document_position(geom::Position window_position) const;
Expand Down