Skip to content

Commit

Permalink
Cleaned up pos save code + fixed pos not saved when double size is ac…
Browse files Browse the repository at this point in the history
…tive.
  • Loading branch information
DashTheDev committed Sep 1, 2024
1 parent ad77839 commit 0eba582
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
38 changes: 12 additions & 26 deletions spotifyamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,7 @@ bool MainWindow::Load() {
InitThreading();
RegisterForGlobalHotkeys();

std::string leftKey("window_"+std::to_string(id_)+"_l");
std::string topKey("window_"+std::to_string(id_)+"_t");
int left(PrefReadInt(0, leftKey.c_str()));
int top(PrefReadInt(0, topKey.c_str()));
Move(left, top);
LoadPosition(0, 0);

bool compact = PrefReadBool(false, "compact");
compact_ = !compact;
Expand Down Expand Up @@ -851,8 +847,6 @@ void MainWindow::Paint() {

void MainWindow::SetDoubleSize(bool v) {
PlatformWindow::SetDoubleSize(v);
eq_window_->SetDoubleSize(v);
PrefWriteInt(v, "double_size");
SetVisualizer(24, 43, 76, 16);
}

Expand Down Expand Up @@ -986,7 +980,12 @@ void MainWindow::Perform(int cmd) {
if (const char *s = PlatformReadClipboard())
TspPlayerPlayContext(tsp_, s, NULL, 0);
break;
case CMD_DOUBLE_SIZE: SetDoubleSize(!double_size()); break;
case CMD_DOUBLE_SIZE: {
bool is_double_size(!double_size());
PrefWriteInt(is_double_size, "double_size");
SetDoubleSize(is_double_size);
eq_window_->SetDoubleSize(is_double_size);
} break;
case CMD_ALWAYS_ON_TOP: SetAlwaysOnTop(!always_on_top()); break;
case CMD_COMPACT: SetCompact(!compact_); break;
case CMD_LOGIN: ShowLoginDialog(); break;
Expand Down Expand Up @@ -1531,12 +1530,7 @@ PlaylistWindow::~PlaylistWindow() {
}

void PlaylistWindow::Load() {
std::string leftKey("window_"+std::to_string(id_)+"_l");
std::string topKey("window_"+std::to_string(id_)+"_t");
int left(PrefReadInt(0, leftKey.c_str()));
int top(PrefReadInt(0, topKey.c_str()));
Move(left, top);

LoadPosition(main_window.screen_rect()->left, main_window.screen_rect()->bottom);
SetCompact(PrefReadBool(false, "pl.compact"));
font_size_ = PrefReadInt(10, "pl.font_size");
row_height_ = 13;
Expand Down Expand Up @@ -2028,12 +2022,8 @@ void EqWindow::Load() {
gain_mode_ = PrefReadInt(0, "eq.gain_mode");
CopyToTsp();

std::string leftKey("window_"+std::to_string(id_)+"_l");
std::string topKey("window_"+std::to_string(id_)+"_t");
int left(PrefReadInt(0, leftKey.c_str()));
int top(PrefReadInt(0, topKey.c_str()));
Move(left, top);

LoadPosition(main_window.screen_rect()->right, main_window.screen_rect()->top);
SetDoubleSize(PrefReadBool(false, "double_size"));
SetCompact(PrefReadBool(false, "eq.compact"));
}

Expand Down Expand Up @@ -2618,14 +2608,9 @@ void CoverArtWindow::SetImage(const char* image) {
#include <vector>

void CoverArtWindow::Load() {
LoadPosition(main_window.screen_rect()->right, main_window.screen_rect()->bottom);
if (image_ == "") { return; }

std::string leftKey("window_"+std::to_string(id_)+"_l");
std::string topKey("window_"+std::to_string(id_)+"_t");
int left(PrefReadInt(0, leftKey.c_str()));
int top(PrefReadInt(0, topKey.c_str()));
Move(left, top);

PlatformDeleteBitmap(bitmap_);
bitmap_ = PlatformLoadBitmapFromBuf(buffer_.data(), buffer_.size());
}
Expand Down Expand Up @@ -2710,6 +2695,7 @@ PlatformWindow *InitSpotamp(int argc, char **argv) {

eq_window.Load();
playlist_window.Load();
coverart_window.Load();

InitVisualizer();

Expand Down
2 changes: 1 addition & 1 deletion spotifyamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class CoverArtWindow : public GenWindow {
explicit CoverArtWindow(MainWindow* main_window);
virtual ~CoverArtWindow();

void Load();
virtual void Paint();
virtual void OnClose();

Expand All @@ -302,7 +303,6 @@ class CoverArtWindow : public GenWindow {
void GotImagePart(TspImageDownloadResult* part);

private:
void Load();

MainWindow* main_window_;
std::string image_;
Expand Down
23 changes: 23 additions & 0 deletions window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ void PlatformWindowBase<PlatformWindow>::Create(PlatformWindow *owner_window) {
}


template<>
const char *PlatformWindowBase<PlatformWindow>::GetPositionKey(const char *side) {
static char leftKey[12];
snprintf(leftKey, sizeof(leftKey), "window_%d_%s", id_, side);
return leftKey;
}


template<>
void PlatformWindowBase<PlatformWindow>::SavePosition() {
PrefWriteInt(screen_rect_.left, GetPositionKey("l"));
PrefWriteInt(screen_rect_.top, GetPositionKey("t"));
}


template<>
void PlatformWindowBase<PlatformWindow>::LoadPosition(int def_left, int def_top) {
int left(PrefReadInt(def_left, GetPositionKey("l")));
int top(PrefReadInt(def_top, GetPositionKey("t")));
Move(left, top);
}


template<>
void PlatformWindowBase<PlatformWindow>::SetDoubleSize(bool v) {
if (v != double_size_) {
Expand Down
7 changes: 7 additions & 0 deletions window.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class PlatformWindowBase {

void Create(T *owner_window);

void SavePosition();
void LoadPosition(int def_left, int def_top);

// Double all pixels
void SetDoubleSize(bool v);
bool double_size() const { return double_size_; }
Expand All @@ -30,6 +33,8 @@ class PlatformWindowBase {
void SetVisible(bool visible);
bool visible() const { return visible_; }

virtual void Move(int l, int t) {}

// Resize window. If you're in doublesize mode, these still return the normal sized size.
void Resize(int w, int h);
int width() const { return width_; }
Expand Down Expand Up @@ -58,6 +63,8 @@ class PlatformWindowBase {
int width_, height_;
Rect screen_rect_;
T *next_, *owner_;
private:
const char *GetPositionKey(const char *side);
};

#if defined(WITH_SDL)
Expand Down
5 changes: 1 addition & 4 deletions window_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,7 @@ void PlatformWindow::MoveAllWindows() {
r->left, r->top,
r->right - r->left, r->bottom - r->top, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
w->need_move_ = false;
std::string leftKey("window_"+std::to_string(w->id_)+"_l");
std::string topKey("window_"+std::to_string(w->id_)+"_t");
PrefWriteInt(r->left, leftKey.c_str());
PrefWriteInt(r->top, topKey.c_str());
w->SavePosition();
}
}
EndDeferWindowPos(dp);
Expand Down

0 comments on commit 0eba582

Please sign in to comment.