Don't change user-defined pos of Splitter when we resize the window

This commit is contained in:
David Capello 2021-04-20 15:14:12 -03:00
parent 660407c9b2
commit 47434ee8f1
2 changed files with 26 additions and 21 deletions

View File

@ -1,5 +1,5 @@
// Aseprite UI Library // Aseprite UI Library
// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello // Copyright (C) 2001-2017 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -32,6 +32,7 @@ using namespace gfx;
Splitter::Splitter(Type type, int align) Splitter::Splitter(Type type, int align)
: Widget(kSplitterWidget) : Widget(kSplitterWidget)
, m_type(type) , m_type(type)
, m_userPos(50)
, m_pos(50) , m_pos(50)
, m_guiscale(guiscale()) , m_guiscale(guiscale())
{ {
@ -41,8 +42,8 @@ Splitter::Splitter(Type type, int align)
void Splitter::setPosition(double pos) void Splitter::setPosition(double pos)
{ {
m_pos = pos; m_userPos = pos;
limitPos(); calcPos();
onPositionChange(); onPositionChange();
invalidate(); invalidate();
@ -111,25 +112,25 @@ bool Splitter::onProcessMessage(Message* msg)
if (align() & HORIZONTAL) { if (align() & HORIZONTAL) {
switch (m_type) { switch (m_type) {
case ByPercentage: case ByPercentage:
m_pos = 100.0 * (mousePos.x - bounds().x) / bounds().w; m_userPos = 100.0 * (mousePos.x - bounds().x) / bounds().w;
break; break;
case ByPixel: case ByPixel:
m_pos = mousePos.x - bounds().x; m_userPos = mousePos.x - bounds().x;
break; break;
} }
} }
else { else {
switch (m_type) { switch (m_type) {
case ByPercentage: case ByPercentage:
m_pos = 100.0 * (mousePos.y - bounds().y) / bounds().h; m_userPos = 100.0 * (mousePos.y - bounds().y) / bounds().h;
break; break;
case ByPixel: case ByPixel:
m_pos = mousePos.y - bounds().y; m_userPos = mousePos.y - bounds().y;
break; break;
} }
} }
limitPos(); calcPos();
onPositionChange(); onPositionChange();
return true; return true;
} }
@ -243,7 +244,7 @@ void Splitter::onResize(ResizeEvent& ev)
int avail; int avail;
setBoundsQuietly(rc); setBoundsQuietly(rc);
limitPos(); calcPos();
Widget* child1 = panel1(); Widget* child1 = panel1();
Widget* child2 = panel2(); Widget* child2 = panel2();
@ -315,15 +316,18 @@ void Splitter::onSizeHint(SizeHintEvent& ev)
void Splitter::onLoadLayout(LoadLayoutEvent& ev) void Splitter::onLoadLayout(LoadLayoutEvent& ev)
{ {
ev.stream() >> m_pos; ev.stream() >> m_userPos;
if (m_pos < 0) m_pos = 0; if (m_userPos < 0) m_userPos = 0;
if (m_type == ByPixel) if (m_type == ByPixel)
m_pos *= m_guiscale; m_userPos *= m_guiscale;
calcPos();
} }
void Splitter::onSaveLayout(SaveLayoutEvent& ev) void Splitter::onSaveLayout(SaveLayoutEvent& ev)
{ {
double pos = (m_type == ByPixel ? m_pos / m_guiscale: m_pos); double pos = (m_type == ByPixel ? m_userPos / m_guiscale:
m_userPos);
ev.stream() << pos; ev.stream() << pos;
} }
@ -350,27 +354,27 @@ Widget* Splitter::panel2() const
return nullptr; return nullptr;
} }
void Splitter::limitPos() void Splitter::calcPos()
{ {
if (align() & HORIZONTAL) { if (align() & HORIZONTAL) {
switch (m_type) { switch (m_type) {
case ByPercentage: case ByPercentage:
m_pos = base::clamp<double>(m_pos, 0, 100); m_pos = base::clamp<double>(m_userPos, 0, 100);
break; break;
case ByPixel: case ByPixel:
if (isVisible()) if (isVisible())
m_pos = base::clamp<double>(m_pos, 0, bounds().w); m_pos = base::clamp<double>(m_userPos, 0, bounds().w);
break; break;
} }
} }
else { else {
switch (m_type) { switch (m_type) {
case ByPercentage: case ByPercentage:
m_pos = base::clamp<double>(m_pos, 0, 100); m_pos = base::clamp<double>(m_userPos, 0, 100);
break; break;
case ByPixel: case ByPixel:
if (isVisible()) if (isVisible())
m_pos = base::clamp<double>(m_pos, 0, bounds().h); m_pos = base::clamp<double>(m_userPos, 0, bounds().h);
break; break;
} }
} }

View File

@ -1,4 +1,5 @@
// Aseprite UI Library // Aseprite UI Library
// Copyright (C) 2021 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello // Copyright (C) 2001-2017 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -18,7 +19,7 @@ namespace ui {
Splitter(Type type, int align); Splitter(Type type, int align);
double getPosition() const { return m_pos; } double getPosition() const { return m_userPos; }
void setPosition(double pos); void setPosition(double pos);
protected: protected:
@ -35,10 +36,10 @@ namespace ui {
private: private:
Widget* panel1() const; Widget* panel1() const;
Widget* panel2() const; Widget* panel2() const;
void limitPos(); void calcPos();
Type m_type; Type m_type;
double m_pos; double m_userPos, m_pos;
int m_guiscale; int m_guiscale;
}; };