mirror of
https://github.com/dortania/OpenCore-Legacy-Patcher.git
synced 2026-04-21 18:40:16 +10:00
GUI: Implement download GUI class
Unifies all download UIs
This commit is contained in:
80
resources/wx_gui/gui_download.py
Normal file
80
resources/wx_gui/gui_download.py
Normal file
@@ -0,0 +1,80 @@
|
||||
|
||||
import wx
|
||||
|
||||
from resources import constants, network_handler, utilities
|
||||
|
||||
|
||||
class DownloadFrame(wx.Frame):
|
||||
"""
|
||||
Update provided frame with download stats
|
||||
"""
|
||||
def __init__(self, parent: wx.Frame, title: str, global_constants: constants.Constants, download_obj: network_handler.DownloadObject, item_name: str, screen_location: tuple = None):
|
||||
|
||||
self.constants: constants.Constants = global_constants
|
||||
self.title: str = title
|
||||
self.parent: wx.Frame = parent
|
||||
self.download_obj: network_handler.DownloadObject = download_obj
|
||||
self.item_name: str = item_name
|
||||
|
||||
self.frame_modal = wx.Dialog(parent, title=title, size=(400, 200))
|
||||
|
||||
self._generate_elements(self.frame_modal)
|
||||
|
||||
|
||||
|
||||
def _generate_elements(self, frame: wx.Frame = None) -> None:
|
||||
|
||||
frame = self if not frame else frame
|
||||
|
||||
title_label = wx.StaticText(frame, label=f"Downloading: {self.item_name}", pos=(-1,5))
|
||||
title_label.SetFont(wx.Font(19, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, ".AppleSystemUIFont"))
|
||||
title_label.Center(wx.HORIZONTAL)
|
||||
|
||||
label_amount = wx.StaticText(frame, label="0.00 B downloaded of 0.00B (0.00%)", pos=(-1, title_label.GetPosition()[1] + title_label.GetSize()[1] + 5))
|
||||
label_amount.SetFont(wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, ".AppleSystemUIFont"))
|
||||
label_amount.Center(wx.HORIZONTAL)
|
||||
|
||||
label_speed = wx.StaticText(frame, label="Average download speed: Unknown", pos=(-1, label_amount.GetPosition()[1] + label_amount.GetSize()[1] + 5))
|
||||
label_speed.SetFont(wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, ".AppleSystemUIFont"))
|
||||
label_speed.Center(wx.HORIZONTAL)
|
||||
|
||||
label_est_time = wx.StaticText(frame, label="Estimated time remaining: Unknown", pos=(-1, label_speed.GetPosition()[1] + label_speed.GetSize()[1] + 5))
|
||||
label_est_time.SetFont(wx.Font(13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, ".AppleSystemUIFont"))
|
||||
label_est_time.Center(wx.HORIZONTAL)
|
||||
|
||||
progress_bar = wx.Gauge(frame, range=100, pos=(-1, label_est_time.GetPosition()[1] + label_est_time.GetSize()[1] + 5), size=(300, 20))
|
||||
progress_bar.Center(wx.HORIZONTAL)
|
||||
|
||||
# Set size of frame
|
||||
frame.SetSize((-1, progress_bar.GetPosition()[1] + progress_bar.GetSize()[1] + 40))
|
||||
frame.ShowWindowModal()
|
||||
|
||||
self.download_obj.download()
|
||||
while self.download_obj.is_active():
|
||||
if self.download_obj.total_file_size == -1:
|
||||
amount_str = f"{utilities.human_fmt(self.download_obj.downloaded_file_size)} downloaded"
|
||||
else:
|
||||
amount_str = f"{utilities.human_fmt(self.download_obj.downloaded_file_size)} downloaded of {utilities.human_fmt(self.download_obj.total_file_size)} ({self.download_obj.get_percent():.2f}%)"
|
||||
label_amount.SetLabel(amount_str)
|
||||
label_amount.Center(wx.HORIZONTAL)
|
||||
|
||||
label_speed.SetLabel(
|
||||
f"Average download speed: {utilities.human_fmt(self.download_obj.get_speed())}/s"
|
||||
)
|
||||
|
||||
label_est_time.SetLabel(
|
||||
f"Estimated time remaining: {utilities.seconds_to_readable_time(self.download_obj.get_time_remaining())}"
|
||||
)
|
||||
|
||||
|
||||
progress_bar.SetValue(int(self.download_obj.get_percent()))
|
||||
wx.GetApp().Yield()
|
||||
|
||||
if self.download_obj.download_complete is False:
|
||||
wx.MessageBox(f"Download failed: \n{self.download_obj.error_msg}", "Error", wx.OK | wx.ICON_ERROR)
|
||||
|
||||
frame.Destroy()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user