Skip to content

Commit

Permalink
refactor & fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLHy0424 committed Nov 11, 2024
1 parent be5ce79 commit 82e3863
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 49 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tag=std
arch=x86_64
version=v5.10.2
.PHONY:init build debug clean
all:init clean build
init:
$(msys2)\\usr\\bin\\pacman.exe -Syyu --noconfirm --needed\
mingw-w64-ucrt-x86_64-toolchain\
Expand Down
58 changes: 36 additions & 22 deletions src/console_ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,29 @@ class console_ui final{
}
};
std::vector<ui_item_> item_;
short height_,width_;
enum console_attrs_op_{v_normal=0,v_lock_text=1,v_lock_all=2};
short width_,height_;
enum console_attrs_op_{val_normal=0,val_lock_text=1,val_lock_all=2};
inline auto show_cursor_(const bool _mode){
CONSOLE_CURSOR_INFO cursor;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor);
cursor.bVisible=_mode;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor);
}
inline auto edit_attrs_(const console_attrs_op_ _mode){
inline auto edit_console_attrs_(const console_attrs_op_ _mode){
DWORD attrs;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),&attrs);
switch(_mode){
case v_normal:{
case val_normal:{
attrs|=ENABLE_QUICK_EDIT_MODE,
attrs|=ENABLE_INSERT_MODE,
attrs|=ENABLE_MOUSE_INPUT;
break;
}case v_lock_text:{
}case val_lock_text:{
attrs&=~ENABLE_QUICK_EDIT_MODE,
attrs&=~ENABLE_INSERT_MODE,
attrs|=ENABLE_MOUSE_INPUT;
break;
}case v_lock_all:{
}case val_lock_all:{
attrs&=~ENABLE_QUICK_EDIT_MODE,
attrs&=~ENABLE_INSERT_MODE,
attrs&=~ENABLE_MOUSE_INPUT;
Expand Down Expand Up @@ -174,18 +174,18 @@ class console_ui final{
}
}
}
inline auto exec_fn_(const MOUSE_EVENT_RECORD &_mouse_event){
inline auto call_func_(const MOUSE_EVENT_RECORD &_mouse_event){
bool is_exit{};
for(auto &line:item_){
if(line==_mouse_event.dwMousePosition){
if(line.func!=nullptr){
cls_();
line.set_color(line.default_color);
show_cursor_(false);
edit_attrs_(v_lock_all);
edit_console_attrs_(val_lock_all);
is_exit=line.func(args{_mouse_event,this});
show_cursor_(false);
edit_attrs_(v_lock_text);
edit_console_attrs_(val_lock_text);
init_pos_();
}
break;
Expand Down Expand Up @@ -233,14 +233,22 @@ class console_ui final{
EnableMenuItem(
GetSystemMenu(GetConsoleWindow(),FALSE),
SC_CLOSE,
MF_BYCOMMAND|(_close_window_ctrl)?(MF_ENABLED):(MF_DISABLED)|MF_GRAYED
MF_BYCOMMAND|(
(_close_window_ctrl)
?(MF_ENABLED)
:(MF_DISABLED)
)|MF_GRAYED
);
SetLayeredWindowAttributes(GetConsoleWindow(),0,_transparency,LWA_ALPHA);
return *this;
}
inline auto &lock(const bool _is_hide_cursor,const bool _is_lock_text){
show_cursor_(!_is_hide_cursor);
edit_attrs_((_is_lock_text)?(v_lock_all):(v_normal));
edit_console_attrs_(
(_is_lock_text)
?(val_lock_all)
:(val_normal)
);
return *this;
}
inline auto &add(
Expand All @@ -253,7 +261,9 @@ class console_ui final{
ui_item_{
_text,
_default_color,
(_func==nullptr)?(_default_color):(_highlight_color),
(_func==nullptr)
?(_default_color)
:(_highlight_color),
_func
}
);
Expand All @@ -271,7 +281,9 @@ class console_ui final{
ui_item_{
_text,
_default_color,
(_func==nullptr)?(_default_color):(_highlight_color),
(_func==nullptr)
?(_default_color)
:(_highlight_color),
_func
}
);
Expand All @@ -287,7 +299,9 @@ class console_ui final{
item_.at(_index)=ui_item_{
_text,
_default_color,
(_func==nullptr)?(_default_color):(_highlight_color),
(_func==nullptr)
?(_default_color)
:(_highlight_color),
_func
};
return *this;
Expand All @@ -306,7 +320,7 @@ class console_ui final{
}
inline auto &show(){
show_cursor_(false);
edit_attrs_(v_lock_text);
edit_console_attrs_(val_lock_text);
MOUSE_EVENT_RECORD mouse_event;
init_pos_();
bool is_exit{};
Expand All @@ -318,7 +332,7 @@ class console_ui final{
break;
}case CONSOLE_MOUSE_CLICK:{
if((mouse_event.dwButtonState)&&(mouse_event.dwButtonState!=CONSOLE_MOUSE_WHEEL)){
is_exit=exec_fn_(mouse_event);
is_exit=call_func_(mouse_event);
}
break;
}
Expand All @@ -330,18 +344,18 @@ class console_ui final{
}
inline explicit console_ui():
item_{},
height_{},
width_{}
width_{},
height_{}
{}
inline explicit console_ui(const console_ui &_obj):
item_{_obj.item_},
height_{},
width_{}
width_{},
height_{}
{}
inline explicit console_ui(const console_ui &&_obj):
item_{std::move(_obj.item_)},
height_{},
width_{}
width_{},
height_{}
{}
inline ~console_ui(){}
};
6 changes: 5 additions & 1 deletion src/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
#endif
#define INFO_REPO_URL "https://github.com/MaxLHy0424/CRCSN"
#define INFO_DEVELOPER "MaxLHy0424"
#define INFO_LICENSE "MIT License"
#define INFO_LICENSE "MIT License"
#ifdef _PREVIEW_
#define WINDOW_WIDTH 50
#define WINDOW_HEIGHT 25
#endif
10 changes: 6 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifdef _PREVIEW_
auto main()->int{
console_ui ui;
ui.set_console(936,"CRCSN",50,25,true,false,true,255)
ui.set_console(936,"CRCSN",WINDOW_WIDTH,WINDOW_HEIGHT,true,false,true,255)
.lock(true,true);
puts(":: 检测运行权限.");
if(!mod::is_run_as_admin()){
Expand Down Expand Up @@ -103,12 +103,14 @@ auto main(const int _argc,const char *const _args[])->int{
.set_console(
936,
"CRCSN",
50,
25,
WINDOW_WIDTH,
WINDOW_HEIGHT,
true,
false,
!config_data.hide_window_close_ctrl,
(config_data.enhanced_window)?(230):(255)
(config_data.enhanced_window)
?(230)
:(255)
)
#endif
.show()
Expand Down
47 changes: 25 additions & 22 deletions src/mod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ namespace mod{
system("mode con cols=50 lines=25");
SetLayeredWindowAttributes(
GetConsoleWindow(),0,
(config_data.translucent_window)?(230):(255),
(config_data.translucent_window)
?(230)
:(255),
LWA_ALPHA
);
}
Expand Down Expand Up @@ -139,7 +141,6 @@ namespace mod{
.add(" " INFO_VERSION)
.add("\n[仓库]\n")
#ifdef _PREVIEW_
.add(" (i) 点击访问 URL.\n")
.add(" " INFO_REPO_URL,std::move(visit_repo_webpage))
#else
.add(" " INFO_REPO_URL)
Expand All @@ -157,12 +158,14 @@ namespace mod{
console_ui{}.set_console(
936,
"CRCSN",
50,
25,
WINDOW_WIDTH,
WINDOW_HEIGHT,
true,
false,
!config_data.hide_window_close_ctrl,
(config_data.enhanced_window)?(230):(255)
(config_data.enhanced_window)
?(230)
:(255)
);
#else
if(!config_data.window_ctrls){
Expand Down Expand Up @@ -194,32 +197,32 @@ namespace mod{
sys_rule::customize.svc.clear();
std::string line;
enum{
v_unknown=-1,
v_settings=0,
v_rule_exe=1,
v_rule_svc=2,
}config_item{v_unknown};
val_unknown=-1,
val_settings=0,
val_rule_exe=1,
val_rule_svc=2,
}config_item{val_unknown};
while(std::getline(config_file,line)){
if((line.empty())||(line.front()=='#')){
continue;
}
if(line=="<settings>"){
config_item=v_settings;
config_item=val_settings;
continue;
}else if(line=="<rule_exe>"){
config_item=v_rule_exe;
config_item=val_rule_exe;
continue;
}else if(line=="<rule_svc>"){
config_item=v_rule_svc;
config_item=val_rule_svc;
continue;
}else if((line.front()=='<')&&(line.back()=='>')){
config_item=v_unknown;
config_item=val_unknown;
continue;
}
switch(config_item){
case v_unknown:{
case val_unknown:{
break;
}case v_settings:{
}case val_settings:{
if(is_reload_){
continue;
}
Expand All @@ -231,10 +234,10 @@ namespace mod{
config_data.enhanced_op=true;
}
break;
}case v_rule_exe:{
}case val_rule_exe:{
sys_rule::customize.exe.emplace_back(line.c_str());
break;
}case v_rule_svc:{
}case val_rule_svc:{
sys_rule::customize.svc.emplace_back(line.c_str());
break;
}
Expand Down Expand Up @@ -268,7 +271,7 @@ namespace mod{
}
}
text.append("<rule_svc>\n");
if(!sys_rule::customize.exe.empty()){
if(!sys_rule::customize.svc.empty()){
for(const auto &item:sys_rule::customize.svc){
text.append(item.get()).push_back('\n');
}
Expand Down Expand Up @@ -296,7 +299,7 @@ namespace mod{
}};
console_ui ui;
ui.add(" [ 配 置 ]\n\n")
.add(" (i) 可通过 <rule_exe> 与 <rule_svc> 自定义规则.\n")
.add(" (i) 相关信息可参阅文档.\n")
.add(" < 同步配置并返回 ",std::move(sync),CONSOLE_TEXT_RED)
.add(" > 打开配置文件 ",std::move(open_config_file))
.add("\n[隐藏窗口关闭控件 (下次启动时生效)]\n")
Expand Down Expand Up @@ -393,9 +396,9 @@ namespace mod{
break;
}
}
printf(":: 执行命令.\n%s\n",std::string(50,'-').c_str());
printf(":: 执行命令.\n%s\n",std::string(WINDOW_WIDTH,'-').c_str());
system(cmd.c_str());
printf("%s\n::释放内存.",std::string(50,'-').c_str());
printf("%s\n::释放内存.",std::string(WINDOW_WIDTH,'-').c_str());
return false;
}
inline explicit sys_op(const char _mode,const sys_rule::base &_rule):
Expand Down

0 comments on commit 82e3863

Please sign in to comment.