Skip to content

Commit

Permalink
add option to remember window pos and size
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 22, 2023
1 parent 4ec82ff commit ec989ae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
14 changes: 14 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ class Config {
GlyphRange_Vietnamese = 1 << 5,
};

// interface
std::string Theme = "light";
float Scale = 0;

// font
std::string FontPath;
int FontSize = 13;
int glyphRange = GlyphRange_Default;

// mpv
bool mpvConfig = false;
bool mpvWid = false;
bool watchLater = false;

// window
bool winSave = false;
int winX = 0;
int winY = 0;
int winW = 0;
int winH = 0;

// log
std::string logLevel = "no";
int logLimit = 500;

Expand Down
10 changes: 10 additions & 0 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ void Config::load() {
inipp::get_value(ini.sections["mpv"], "config", mpvConfig);
inipp::get_value(ini.sections["mpv"], "wid", mpvWid);
inipp::get_value(ini.sections["mpv"], "watch-later", watchLater);
inipp::get_value(ini.sections["window"], "save", winSave);
inipp::get_value(ini.sections["window"], "x", winX);
inipp::get_value(ini.sections["window"], "y", winY);
inipp::get_value(ini.sections["window"], "w", winW);
inipp::get_value(ini.sections["window"], "h", winH);
inipp::get_value(ini.sections["debug"], "log-level", logLevel);
inipp::get_value(ini.sections["debug"], "log-limit", logLimit);
}
Expand All @@ -42,6 +47,11 @@ void Config::save() {
ini.sections["mpv"]["config"] = format("{}", mpvConfig);
ini.sections["mpv"]["wid"] = format("{}", mpvWid);
ini.sections["mpv"]["watch-later"] = format("{}", watchLater);
ini.sections["window"]["save"] = format("{}", winSave);
ini.sections["window"]["x"] = std::to_string(winX);
ini.sections["window"]["y"] = std::to_string(winY);
ini.sections["window"]["w"] = std::to_string(winW);
ini.sections["window"]["h"] = std::to_string(winH);
ini.sections["debug"]["log-level"] = logLevel;
ini.sections["debug"]["log-limit"] = std::to_string(logLimit);

Expand Down
3 changes: 2 additions & 1 deletion source/views/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Settings::draw() {

void Settings::drawGeneralTab() {
if (ImGui::BeginTabItem("General")) {
ImGui::Text("MPV");
ImGui::Text("Core");
ImGui::Indent();
ImGui::Checkbox("Use mpv's config dir*", &config->mpvConfig);
ImGui::SameLine();
Expand All @@ -48,6 +48,7 @@ void Settings::drawGeneralTab() {
ImGui::Checkbox("Remember playback progress on exit", &config->watchLater);
ImGui::SameLine();
ImGui::HelpMarker("Exit mpv with the quit-watch-later command.");
ImGui::Checkbox("Remember window position and size on exit", &config->winSave);
ImGui::Unindent();
ImGui::Text("Debug");
ImGui::SameLine();
Expand Down
16 changes: 15 additions & 1 deletion source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ bool Window::run(OptionParser& parser) {

renderThread.join();

if (config.winSave) {
glfwGetWindowPos(window, &config.winX, &config.winY);
glfwGetWindowSize(window, &config.winW, &config.winH);
config.save();
}

return true;
}

Expand Down Expand Up @@ -142,11 +148,19 @@ void Window::initGLFW(const char* title) {
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
width = std::max((int)(mode->width * 0.4), 600);
height = std::max((int)(mode->height * 0.4), 400);
int posX = (mode->width - width) / 2;
int posY = (mode->height - height) / 2;
if (config.winSave) {
if (config.winW > 0) width = config.winW;
if (config.winH > 0) height = config.winH;
if (config.winX >= 0) posX = config.winX;
if (config.winY >= 0) posY = config.winY;
}

window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (window == nullptr) throw std::runtime_error("Failed to create window!");
glfwSetWindowSizeLimits(window, 640, 480, GLFW_DONT_CARE, GLFW_DONT_CARE);
glfwSetWindowPos(window, (mode->width - width) / 2, (mode->height - height) / 2);
glfwSetWindowPos(window, posX, posY);

glfwSetWindowUserPointer(window, this);
glfwMakeContextCurrent(window);
Expand Down

0 comments on commit ec989ae

Please sign in to comment.