ENH: optimize media file user interface

Change-Id: Ic16133fd366fcbf41dd0c9535907786754186857
This commit is contained in:
chunmao.guo 2022-10-09 11:11:47 +08:00 committed by Lane.Wei
parent aa2129280d
commit 37723752cd
8 changed files with 79 additions and 36 deletions

View File

@ -288,7 +288,7 @@ void AppConfig::set_defaults()
if (get("backup_interval").empty()) {
set("backup_interval", "10");
}
if (get("curr_bed_type").empty()) {
set("curr_bed_type", "0");
}

View File

@ -8,6 +8,9 @@
#include <wx/dcgraph.h>
#ifdef __APPLE__
#include "../Utils/MacDarkMode.hpp"
#endif
BEGIN_EVENT_TABLE(Slic3r::GUI::ImageGrid, wxPanel)
@ -145,24 +148,19 @@ void Slic3r::GUI::ImageGrid::DoAction(size_t index, int action)
auto &file = m_file_sys->GetFile(index);
if (file.IsDownload() && file.progress >= -1) {
if (file.progress >= 100) {
#ifdef __WXMSW__
wxExecute("cmd /c start " + from_u8(m_save_path + "\\" + file.name), wxEXEC_HIDE_CONSOLE);
#ifdef __WIN32__
wxExecute(L"explorer.exe /select," + from_u8(file.path));
#elif __APPLE__
openFolderForFile(from_u8(file.path));
#else
wxShell("open " + m_save_path + "/" + file.name);
#endif
} else {
m_file_sys->DownloadCancel(index);
}
return;
}
if (m_save_path.empty()) {
wxDirDialog dlg(NULL, _L("Choose save directory"), "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
if (dlg.ShowModal() == wxID_CANCEL)
return;
m_save_path = dlg.GetPath().ToUTF8().data();
}
}
m_file_sys->DownloadFiles(index, m_save_path);
m_file_sys->DownloadFiles(index, wxGetApp().app_config->get("download_path"));
}
}
@ -351,8 +349,12 @@ void Slic3r::GUI::ImageGrid::changedEvent(wxCommandEvent& evt)
{
evt.Skip();
BOOST_LOG_TRIVIAL(info) << "ImageGrid::changedEvent: " << evt.GetEventType() << " index: " << evt.GetInt() << " name: " << evt.GetString() << " extra: " << evt.GetExtraLong();
if (evt.GetEventType() == EVT_MODE_CHANGED
|| evt.GetEventType() == EVT_FILE_CHANGED)
if (evt.GetEventType() == EVT_FILE_CHANGED) {
if (evt.GetInt() == -1)
m_file_sys->DownloadCheckFiles(wxGetApp().app_config->get("download_path"));
UpdateFileSystem();
}
else if (evt.GetEventType() == EVT_MODE_CHANGED)
UpdateFileSystem();
else
Refresh();
@ -480,11 +482,12 @@ void ImageGrid::render(wxDC& dc)
dc.DrawBitmap(m_mask, pt);
}
}
bool show_download_state_always = true;
// Draw checked icon
if (m_selecting)
if (m_selecting && !show_download_state_always)
dc.DrawBitmap(file.IsSelect() ? m_checked_icon.bmp() : m_unchecked_icon.bmp(),
pt + wxPoint{10, m_image_size.GetHeight() - m_checked_icon.GetBmpHeight() - 10});
// can' handle alpha
// can't handle alpha
// dc.GradientFillLinear({pt.x, pt.y, m_image_size.GetWidth(), 60}, wxColour(0x6F, 0x6F, 0x6F, 0x99), wxColour(0x6F, 0x6F, 0x6F, 0), wxBOTTOM);
else if (m_file_sys->GetGroupMode() == PrinterFileSystem::G_NONE) {
wxString nonHoverText;
@ -500,7 +503,7 @@ void ImageGrid::render(wxDC& dc)
nonHoverText = _L("Download failed");
states = StateColor::Checked;
} else if (file.progress >= 100) {
secondAction = _L("Open");
secondAction = _L("Open Folder");
nonHoverText = _L("Download finished");
} else {
secondAction = _L("Cancel");
@ -514,6 +517,9 @@ void ImageGrid::render(wxDC& dc)
} else if (!nonHoverText.IsEmpty()) {
renderButtons(dc, {(wxChar const *) nonHoverText, nullptr}, rect, -1, states);
}
if (m_selecting && show_download_state_always)
dc.DrawBitmap(file.IsSelect() ? m_checked_icon.bmp() : m_unchecked_icon.bmp(),
pt + wxPoint{10, m_image_size.GetHeight() - m_checked_icon.GetBmpHeight() - 10});
} else {
auto date = wxDateTime((time_t) file.time).Format(_L(formats[m_file_sys->GetGroupMode()]));
dc.DrawText(date, pt + wxPoint{24, 16});

View File

@ -96,8 +96,6 @@ private:
wxBitmap m_status_icon;
wxString m_status_msg;
std::string m_save_path;
ScalableBitmap m_checked_icon;
ScalableBitmap m_unchecked_icon;
StateColor m_buttonBackgroundColor;

View File

@ -82,7 +82,7 @@ MediaFilePanel::MediaFilePanel(wxWindow * parent)
m_button_delete->SetToolTip(L("Delete selected files from printer."));
m_button_download = new ::Button(m_manage_panel, _L("Download"));
m_button_download->SetToolTip(L("Download selected files from printer."));
m_button_management = new ::Button(m_manage_panel, _L("Management"));
m_button_management = new ::Button(m_manage_panel, _L("Select"));
m_button_management->SetToolTip(L("Batch manage files."));
for (auto b : {m_button_delete, m_button_download, m_button_management}) {
b->SetBackgroundColor(StateColor());
@ -163,18 +163,26 @@ MediaFilePanel::MediaFilePanel(wxWindow * parent)
b->GetEventHandler()->ProcessEvent(e);
}
// File management
m_button_management->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this](auto &e) {
e.Skip();
bool selecting = !m_image_grid->IsSelecting();
auto set_selecting = [this](bool selecting) {
m_image_grid->SetSelecting(selecting);
m_button_management->SetLabel(selecting ? _L("Finish") : _L("Management"));
m_button_management->SetLabel(selecting ? _L("Cancel") : _L("Select"));
m_manage_panel->GetSizer()->Show(m_button_download, selecting);
m_manage_panel->GetSizer()->Show(m_button_delete, selecting);
m_manage_panel->Layout();
};
// File management
m_button_management->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this, set_selecting](auto &e) {
e.Skip();
set_selecting(!m_image_grid->IsSelecting());
});
m_button_download->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this, set_selecting](auto &e) {
m_image_grid->DoActionOnSelection(1);
set_selecting(false);
});
m_button_delete->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this, set_selecting](auto &e) {
m_image_grid->DoActionOnSelection(0);
set_selecting(false);
});
m_button_download->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this](auto &e) { m_image_grid->DoActionOnSelection(1); });
m_button_delete->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [this](auto &e) { m_image_grid->DoActionOnSelection(0); });
auto onShowHide = [this](auto &e) {
e.Skip();

View File

@ -10,7 +10,9 @@
#include <cstring>
#ifndef NDEBUG
//#define PRINTER_FILE_SYSTEM_TEST
#endif
wxDEFINE_EVENT(EVT_STATUS_CHANGED, wxCommandEvent);
wxDEFINE_EVENT(EVT_MODE_CHANGED, wxCommandEvent);
@ -38,7 +40,7 @@ PrinterFileSystem::PrinterFileSystem()
for (int i = 0; i < 800; ++i) {
auto name = wxString::Format(L"img-%03d.jpg", i + 1);
wxImage im(L"D:\\work\\pic\\" + name);
m_file_list.push_back({name.ToUTF8().data(), time.GetTicks(), 0, im, i < 20 ? FF_DOWNLOAD : 0, i * 10 - 40});
m_file_list.push_back({name.ToUTF8().data(), time.GetTicks(), 26937, im, i < 20 ? FF_DOWNLOAD : 0, i * 10 - 40});
time.Add(wxDateSpan::Days(-1));
}
BuildGroups();
@ -160,6 +162,7 @@ void PrinterFileSystem::DownloadFiles(size_t index, std::string const &path)
if ((file.flags & FF_DOWNLOAD) != 0 && file.progress >= 0) continue;
file.flags |= FF_DOWNLOAD;
file.progress = -1;
file.path = (boost::filesystem::path(path) / file.name).string();
++n;
}
if (n == 0) return;
@ -171,9 +174,25 @@ void PrinterFileSystem::DownloadFiles(size_t index, std::string const &path)
return;
file.flags |= FF_DOWNLOAD;
file.progress = -1;
file.path = (boost::filesystem::path(path) / file.name).string();
}
if ((m_task_flags & FF_DOWNLOAD) == 0)
DownloadNextFile(path);
DownloadNextFile();
}
void PrinterFileSystem::DownloadCheckFiles(std::string const &path)
{
for (size_t i = 0; i < m_file_list.size(); ++i) {
auto &file = m_file_list[i];
if ((file.flags & FF_DOWNLOAD) != 0 && file.progress >= 0) continue;
auto path2 = boost::filesystem::path(path) / file.name;
boost::system::error_code ec;
if (boost::filesystem::file_size(path2, ec) == file.size) {
file.flags |= FF_DOWNLOAD;
file.progress = 100;
file.path = path2.string();
}
}
}
void PrinterFileSystem::DownloadCancel(size_t index)
@ -346,12 +365,12 @@ void PrinterFileSystem::DeleteFilesContinue()
// TODO:
for (size_t i = indexes.size() - 1; i != size_t(-1); --i)
FileRemoved(indexes[i], names[i]);
SendChangedEvent(EVT_FILE_CHANGED);
SendChangedEvent(EVT_FILE_CHANGED, indexes.size());
DeleteFilesContinue();
});
}
void PrinterFileSystem::DownloadNextFile(std::string const &path)
void PrinterFileSystem::DownloadNextFile()
{
size_t index = size_t(-1);
for (size_t i = 0; i < m_file_list.size(); ++i) {
@ -378,7 +397,7 @@ void PrinterFileSystem::DownloadNextFile(std::string const &path)
std::shared_ptr<Download> download(new Download);
download->index = index;
download->name = m_file_list[index].name;
download->path = path;
download->path = m_file_list[index].path;
m_task_flags |= FF_DOWNLOAD;
m_download_seq = SendRequest<Progress>(
FILE_DOWNLOAD, req,
@ -388,7 +407,7 @@ void PrinterFileSystem::DownloadNextFile(std::string const &path)
prog.size = resp["offset"];
prog.total = resp["total"];
if (prog.size == 0) {
download->ofs.open(download->path + "/" + download->name, std::ios::binary);
download->ofs.open(download->path, std::ios::binary);
if (!download->ofs) return FILE_OPEN_ERR;
}
// receive data
@ -429,10 +448,10 @@ void PrinterFileSystem::DownloadNextFile(std::string const &path)
file.flags &= ~FF_DOWNLOAD;
else if (file.progress != progress) {
file.progress = progress;
SendChangedEvent(EVT_DOWNLOAD, download->index, m_file_list[download->index].name, data.size);
SendChangedEvent(EVT_DOWNLOAD, download->index, file.path, data.size);
}
}
if (result != CONTINUE) DownloadNextFile(download->path);
if (result != CONTINUE) DownloadNextFile();
});
}
@ -790,7 +809,9 @@ void PrinterFileSystem::Reconnect(boost::unique_lock<boost::mutex> &l, int resul
}
m_status = Status::ListSyncing;
SendChangedEvent(EVT_STATUS_CHANGED, m_status);
#ifndef PRINTER_FILE_SYSTEM_TEST
#ifdef PRINTER_FILE_SYSTEM_TEST
PostCallback([this] { SendChangedEvent(EVT_FILE_CHANGED); });
#else
PostCallback([this] { ListAllFiles(); });
#endif
}

View File

@ -95,6 +95,7 @@ public:
wxBitmap thumbnail;
int flags = 0;
int progress = -1; // -1: waiting
std::string path;
bool IsSelect() const { return flags & FF_SELECT; }
bool IsDownload() const { return flags & FF_DOWNLOAD; }
@ -124,6 +125,8 @@ public:
void DownloadFiles(size_t index, std::string const &path);
void DownloadCheckFiles(std::string const &path);
void DownloadCancel(size_t index);
size_t GetCount() const;
@ -164,7 +167,7 @@ private:
void DeleteFilesContinue();
void DownloadNextFile(std::string const &path);
void DownloadNextFile();
void UpdateFocusThumbnail();

View File

@ -14,6 +14,7 @@ void WKWebView_evaluateJavaScript(void * web, wxString const & script, void (*ca
void set_tag_when_enter_full_screen(bool isfullscreen);
void set_title_colour_after_set_title(void * window);
void initGestures(void * view, wxEvtHandler * handler);
void openFolderForFile(wxString const & file);
#endif

View File

@ -92,6 +92,12 @@ void WKWebView_evaluateJavaScript(void * web, wxString const & script, void (*ca
}];
}
void openFolderForFile(wxString const & file)
{
NSArray *fileURLs = [NSArray arrayWithObjects:wxCFStringRef(file).AsNSString(), /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
}
}
}