Skip to content

Commit

Permalink
Added background color change
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiurgiu committed Mar 17, 2022
1 parent 88e302c commit 65e346c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
52 changes: 52 additions & 0 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Database::Database():db(nullptr,connection_deleter())
DB_ERR_CHECK("Cannot create table SCHEMA_VERSION");
rc = sqlite3_exec(db.get(),"CREATE TABLE IF NOT EXISTS MEDIA_DOMAINS(DOMAIN TEXT)",nullptr,nullptr,nullptr);
DB_ERR_CHECK("Cannot create table MEDIA_DOMAINS");
rc = sqlite3_exec(db.get(),"CREATE TABLE IF NOT EXISTS COLORS(NAME TEXT, RED FLOAT, GREEN FLOAT, BLUE FLOAT, ALPHA FLOAT)",nullptr,nullptr,nullptr);
DB_ERR_CHECK("Cannot create table COLORS");

int version = getSchemaVersion();
if(version < 1)
Expand Down Expand Up @@ -107,6 +109,56 @@ void Database::incrementSchemaVersion(int version)
DB_ERR_CHECK("Cannot update version");
}
}
void Database::setColor(const std::string& name, const std::array<float,4>& col)
{
std::unique_ptr<sqlite3_stmt,statement_finalizer> stmt;
sqlite3_stmt *stmt_ptr = nullptr;
int rc = sqlite3_prepare_v2(db.get(),"DELETE FROM COLORS WHERE NAME=?",-1,&stmt_ptr, nullptr);
stmt.reset(stmt_ptr);
DB_ERR_CHECK("Cannot delete "+name);
rc = sqlite3_bind_text(stmt.get(),1,name.c_str(),-1,nullptr);
DB_ERR_CHECK("Cannot bind values to "+name);
rc = sqlite3_step(stmt.get());
DB_ERR_CHECK("Cannot insert "+name);
stmt.reset();

stmt_ptr = nullptr;
rc = sqlite3_prepare_v2(db.get(),"INSERT INTO COLORS(NAME,RED,GREEN,BLUE,ALPHA) VALUES(?,?,?,?,?)",-1,&stmt_ptr, nullptr);
stmt.reset(stmt_ptr);
DB_ERR_CHECK("Cannot insert "+name);
rc = sqlite3_bind_text(stmt.get(),1,name.c_str(),-1,nullptr);
DB_ERR_CHECK("Cannot bind values to "+name);
int colIndex = 2;
for(const auto& v : col)
{
rc = sqlite3_bind_double(stmt.get(),colIndex,v);
DB_ERR_CHECK("Cannot bind values to "+name);
colIndex++;
}
rc = sqlite3_step(stmt.get());
DB_ERR_CHECK("Cannot insert "+name);
}
std::array<float,4> Database::getColor(const std::string& name, const std::array<float,4>& defaultColor) const
{
auto value = defaultColor;
std::unique_ptr<sqlite3_stmt,statement_finalizer> stmt;
sqlite3_stmt *stmt_ptr;
int rc = sqlite3_prepare_v2(db.get(),"SELECT RED,GREEN,BLUE,ALPHA FROM COLORS WHERE NAME=?",-1,&stmt_ptr, nullptr);
stmt.reset(stmt_ptr);
DB_ERR_CHECK("Cannot find "+name);
rc = sqlite3_bind_text(stmt.get(),1,name.c_str(),-1,nullptr);
DB_ERR_CHECK("Cannot bind values to "+name);

if(sqlite3_step(stmt.get()) == SQLITE_ROW)
{
value[0] = sqlite3_column_double(stmt.get(),0);
value[1] = sqlite3_column_double(stmt.get(),1);
value[2] = sqlite3_column_double(stmt.get(),2);
value[3] = sqlite3_column_double(stmt.get(),3);
}
return value;
}

std::vector<std::string> Database::getMediaDomains() const
{
std::vector<std::string> domains;
Expand Down
5 changes: 4 additions & 1 deletion src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#include <memory>
#include <vector>
#include <filesystem>

#include <array>
#include <sqlite3.h>
#include "entities.h"


class database_exception : public std::runtime_error
{
using std::runtime_error::runtime_error;
Expand Down Expand Up @@ -57,6 +58,8 @@ class Database
void removeMediaDomain(const std::string& domain);
void setTwitterAuthBearer(const std::string& bearer);
std::optional<std::string> getTwitterAuthBearer();
void setColor(const std::string& name, const std::array<float,4>& col);
std::array<float,4> getColor(const std::string& name, const std::array<float,4>& defaultColor) const;
private:
void setBoolProperty(bool flag, const std::string& propName);
bool getBoolProperty(const std::string& propName, bool defaultValue) const;
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ void runMainLoop(SDL_Window* window,ImGuiIO& io)

desktop->loginCurrentUser();

ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

// Main loop
bool done = false;
while (!done)
Expand Down Expand Up @@ -241,7 +239,8 @@ void runMainLoop(SDL_Window* window,ImGuiIO& io)
// Rendering
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
const auto& backgroundColor = desktop->getBackgroundColor();
glClearColor(backgroundColor.x, backgroundColor.y, backgroundColor.z, backgroundColor.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
Expand All @@ -250,6 +249,7 @@ void runMainLoop(SDL_Window* window,ImGuiIO& io)
done = desktop->quitSelected();
}
}
desktop->saveBackgroundColor();
work.reset();
uiContext.stop();
}
Expand Down
27 changes: 27 additions & 0 deletions src/redditdesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ constexpr auto OPENSUBREDDIT_WINDOW_POPUP_TITLE = "Open Subreddit";
constexpr auto ERROR_WINDOW_POPUP_TITLE = "Error Occurred";
constexpr auto MEDIA_DOMAINS_POPUP_TITLE = "Media Domains Management";
constexpr auto TWITTER_API_BEARER_TITLE = "Twitter API Auth Bearer";
constexpr auto BACKGROUND_COLOR_MENU = "Background Color";
constexpr auto BACKGROUND_COLOR_NAME = "BACKGROUND";
}

RedditDesktop::RedditDesktop(boost::asio::io_context& uiContext):
Expand All @@ -36,6 +38,11 @@ RedditDesktop::RedditDesktop(boost::asio::io_context& uiContext):
showRandomNSFW = db->getShowRandomNSFW();
automaticallyArangeWindowsInGrid = db->getAutoArangeWindowsGrid();
useYoutubeDl = db->getUseYoutubeDownloader();
auto bCol = db->getColor(BACKGROUND_COLOR_NAME,{backgroundColor.x,backgroundColor.y,backgroundColor.z,backgroundColor.w});
backgroundColor.x = bCol[0];
backgroundColor.y = bCol[1];
backgroundColor.z = bCol[2];
backgroundColor.w = bCol[3];
loggingWindow->setupLogging();
loggingWindow->setWindowOpen(showLoggingWindow);
}
Expand Down Expand Up @@ -594,6 +601,22 @@ void RedditDesktop::showMainMenuBar()
{
twitterAPIAuthBearerDialog = true;
}
if(ImGui::BeginMenu(BACKGROUND_COLOR_MENU))
{
float col[3] = { backgroundColor.x, backgroundColor.y,
backgroundColor.z };
if(ImGui::ColorPicker3("###background",col, ImGuiColorEditFlags_AlphaBar|
ImGuiColorEditFlags_NoOptions|
ImGuiColorEditFlags_NoInputs|
ImGuiColorEditFlags_NoSidePreview))
{
backgroundColor.x = col[0];
backgroundColor.y = col[1];
backgroundColor.z = col[2];
backgroundColor.w = 1.f;
}
ImGui::EndMenu();
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Windows"))
Expand Down Expand Up @@ -839,3 +862,7 @@ void RedditDesktop::setSearchResultsNames(names_list names)
searchedNamesList = std::move(names);
searchingSubreddits = false;
}
void RedditDesktop::saveBackgroundColor()
{
Database::getInstance()->setColor(BACKGROUND_COLOR_NAME,{backgroundColor.x,backgroundColor.y,backgroundColor.z,backgroundColor.w});
}
9 changes: 8 additions & 1 deletion src/redditdesktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RedditDesktop : public std::enable_shared_from_this<RedditDesktop>
void loginCurrentUser();
void showDesktop();
void closeWindow();
bool quitSelected()
bool quitSelected() const
{
return shouldQuit;
}
Expand All @@ -35,6 +35,11 @@ class RedditDesktop : public std::enable_shared_from_this<RedditDesktop>
{
appFrameHeight = height;
}
ImVec4 getBackgroundColor() const
{
return backgroundColor;
}
void saveBackgroundColor();
private:
void showMainMenuBar();
void showMenuFile();
Expand Down Expand Up @@ -101,6 +106,8 @@ class RedditDesktop : public std::enable_shared_from_this<RedditDesktop>
AboutWindow aboutWindow;
bool twitterAPIAuthBearerDialog = false;
std::string twitterBearer;
ImVec4 backgroundColor = {0.45f, 0.55f, 0.60f, 1.00f};
bool changeBackgroundColorDialog = false;
};


Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(RD_TEST_SRCS

set(TESTING_RD_CLASSES
${CMAKE_SOURCE_DIR}/src/htmlparser.cpp
${CMAKE_SOURCE_DIR}/src/uri.cpp
)

add_executable(${PROJECT_NAME}
Expand Down

0 comments on commit 65e346c

Please sign in to comment.