When creating an application, you often need to save certain parameters such as the last file used by the user, the last tool used by the user, the current size of the main form or the background color. That's where settings come into play In fact, thanks to its very simple settings management, xtd makes it easy to load and save your resources. Thanks to an automatically generated static class, you can access your settings without worrying about format and location, which may differ depending on the platform used.
Settings declarationUsing the CMake
commands specific to xtd
, simply describe the settings to be added to your application.
Although you can add the setting and setting_include commands (which are the only two commands for adding settings) to your CMakeLists.txt
file, the best thing to do is to create a properties
folder next to the src
folder and describe your resources in the settings.cmake
file. When CMake generates your xtd project, the properties/settings.cmake
file will be detected automatically.
Here's an example of the properties/settings.cmake
file with some specific CMake commands to describe your settings.
# Settings file
# =============
# Remarks
# This file generates the "properties/settings.hpp" file, which is used to access the settings.
# Includes
setting_include("xtd/drawing/point")
setting_include("xtd/drawing/size")
setting_include("xtd/drawing/system_colors")
# User settings
setting(back_color xtd::drawing::color USER "xtd::drawing::system_colors::control()")
setting(location xtd::drawing::point USER "{100, 50}")
setting(size xtd::drawing::size USER "{335, 45}")
# Application settings
setting(text xtd::string APPLICATION "\"Settings example\"")
Settings CMake Commands setting
setting(NAME TYPE SCOPE VALUE)
your_project::properties::settings
.setting_include(SETTIING_INCLUDE_FILE)
The setting_include commands are optional, you can remove them but you must be sure that before including the properties/settings.hpp
file all the types defined in the properties/settings.cmake
file are already known.
Generating your xtd
project with CMake
will automatically generate a static class your_project::properties::settings
containing the static properties for accessing settings. The settings
class will be generated in the propertes/settings.hpp
file. And this file will be automatically added to your project as a source.
#pragma region xtd generated code
#pragma once
#include <xtd/drawing/point>
#include <xtd/drawing/size>
#include <xtd/drawing/system_colors>
#include <xtd/configuration/settings>
namespace application_settings::properties {
class settings : public xtd::object {
public:
settings() noexcept : settings {true} {}
explicit settings(bool load) noexcept {
if (load) reload();
}
settings(settings&&) noexcept = default;
settings(const settings&) noexcept = default;
settings& operator =(const settings&) noexcept = default;
xtd::drawing::color back_color() const noexcept {return back_color_;}
settings& back_color(xtd::drawing::color value) noexcept {
back_color_ = value;
return *this;
}
xtd::drawing::point location() const noexcept {return location_;}
settings& location(xtd::drawing::point value) noexcept {
location_ = value;
return *this;
}
xtd::drawing::size size() const noexcept {return size_;}
settings& size(xtd::drawing::size value) noexcept {
size_ = value;
return *this;
}
xtd::string text() const noexcept {return "Settings example";}
void reload() noexcept {
back_color_ = settings_.read("back_color", back_color_);
location_ = settings_.read("location", location_);
size_ = settings_.read("size", size_);
}
void reset() noexcept {
settings_.reset();
*this = settings {false};
}
void save() noexcept {
settings_.write("back_color", back_color_);
settings_.write("location", location_);
settings_.write("size", size_);
settings_.save();
}
static settings& default_settings() noexcept {
static auto default_settings = settings {};
return default_settings;
}
private:
xtd::configuration::settings settings_;
xtd::drawing::color back_color_ {xtd::drawing::system_colors::control()};
xtd::drawing::point location_ {{100, 50}};
xtd::drawing::size size_ {{335, 45}};
};
}
#pragma endregion
Settings usage
All that's left is to exploit your settings in a way that's simple and transparent to your environment and OS. Just include the inlcude file propertes/settings.hpp
and use its properties.
#include "../properties/settings.hpp"
#include <xtd/forms/application>
#include <xtd/forms/button>
#include <xtd/forms/color_picker>
#include <xtd/forms/form>
using namespace application_settings::properties;
using namespace xtd::forms;
auto main() -> int {
auto main_form = form::create(settings::default_settings().text(), form_start_position::manual);
auto back_color_picker = color_picker::create(main_form, main_form.back_color(), {10, 10}, {75, 25});
back_color_picker.color_picker_changed += [&] {
main_form.back_color(back_color_picker.color());
};
auto save_button = button::create(main_form, "&Save", {90, 10});
save_button.click += [&] {
settings::default_settings().size(main_form.client_size());
settings::default_settings().location(main_form.location());
settings::default_settings().back_color(main_form.back_color());
settings::default_settings().save();
};
auto reload_button = button::create(main_form, "&Reload", {170, 10});
reload_button.click += [&] {
main_form.client_size(settings::default_settings().size());
main_form.location(settings::default_settings().location());
main_form.back_color(settings::default_settings().back_color());
back_color_picker.color(settings::default_settings().back_color());
};
auto reset_button = button::create(main_form, "R&eset", {250, 10});
reset_button.click += [&] {
settings::default_settings().reset();
reload_button.perform_click();
};
reload_button.perform_click();
application::run(main_form);
}
Settings types
xtd settings can handle different types of parameters:
CMakeLists.txt
and don't add a properties/settings.cmake
file, then xtd won't generate anything.The basic settings path can be obtained with the command xtd::environment::get_folder_path(xtd::environment::special_folder::application_data).
Depending on the platform, settings are stored in the following locations:
Platform Path Windows %APPDATA%\company_name\product_name.ini macOS ~/Library/Preferences/company_name/product_name Preferences Linux ~/.config/company_name/product_name.confThe `product_name`` is defined by the xtd::reflection::assembly::product() property of the xtd::reflection::assembly::get_executing_assembly() assembly if it’s not empty; otherwise, it defaults to the filename of the first argument passed to main.
The company_name
is defined by the xtd::reflection::assembly::company() property of the same assembly if not empty; otherwise, it defaults to the same value as product_name
.
Some xtd examples use settings :
and more see xtd.examples.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4