Skip to content

Commit

Permalink
gui: make debug windows right edge sticky
Browse files Browse the repository at this point in the history
Now when the right edge of any debug window in the list is at the edge
of the viewport width, it will stick there through resizes.

There's a bug where if the resize change is enough to "capture" a window
that wasn't intended to be sticky it will capture. Given this didn't
happen much during testing, I don't think the extra state + code
required is worth it for these windows.
  • Loading branch information
GrayHatter committed Feb 8, 2023
1 parent 159409e commit 2165393
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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) {
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

0 comments on commit 2165393

Please sign in to comment.