NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

storage.py (3097B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\shared\systemMenu\theme\storage.py
      2 import carbonui
      3 import contextlib
      4 from clientsettings.setting import _BaseSetting, _UserSettingMixin
      5 from eve.client.script.ui.shared.colorThemes import ColorTheme
      6 from eve.client.script.ui.shared.systemMenu.theme.model import focus_dark_from_focus
      7 
      8 class CustomColorThemeCollection(object):
      9     VERSION = 1
     10 
     11     def __init__(self, themes):
     12         self.themes_by_id = {theme.id:theme for theme in themes}
     13 
     14     @property
     15     def themes(self):
     16         return self.themes_by_id.values()
     17 
     18     def add_or_update(self, theme):
     19         self.themes_by_id[theme.id] = theme
     20 
     21     def remove(self, theme):
     22         del self.themes_by_id[theme.id]
     23 
     24     @classmethod
     25     def deserialize(cls, data):
     26         version = int(data['version'])
     27         return cls(themes=[ deserialize_theme(theme_data) for theme_data in data['themes'] ])
     28 
     29     def serialize(self):
     30         return {'version': self.VERSION,
     31          'themes': [ serialize_theme(theme) for theme in self.themes_by_id.values() ]}
     32 
     33     def __eq__(self, other):
     34         if not isinstance(other, CustomColorThemeCollection):
     35             return False
     36         keys = set(self.themes_by_id.keys())
     37         if keys != set(other.themes_by_id.keys()):
     38             return False
     39         for key in keys:
     40             if self.themes_by_id[key] != other.themes_by_id[key]:
     41                 return False
     42 
     43         return True
     44 
     45 
     46 def serialize_theme(theme):
     47     return {'id': theme.id,
     48      'name': theme.name,
     49      'focus': theme.focus.hex_argb,
     50      'accent': theme.accent.hex_argb,
     51      'alert': theme.alert.hex_argb,
     52      'tint': theme.tint.hex_argb}
     53 
     54 
     55 def deserialize_theme(data):
     56     focus = carbonui.Color.from_hex(data['focus'])
     57     return ColorTheme(theme_id=data['id'], name=data['name'], focus=focus, focus_dark=focus_dark_from_focus(focus), accent=carbonui.Color.from_hex(data['accent']), alert=carbonui.Color.from_hex(data['alert']), tint=carbonui.Color.from_hex(data['tint']))
     58 
     59 
     60 class CustomColorThemesSetting(_BaseSetting, _UserSettingMixin):
     61 
     62     def __init__(self):
     63         super(CustomColorThemesSetting, self).__init__(settings_key='custom_color_themes', default_value=lambda : CustomColorThemeCollection(themes=[]))
     64 
     65     def get(self):
     66         data = super(CustomColorThemesSetting, self).get()
     67         if isinstance(data, CustomColorThemeCollection):
     68             return data
     69         else:
     70             result = CustomColorThemeCollection.deserialize(data)
     71             return result
     72 
     73     @contextlib.contextmanager
     74     def modify(self):
     75         custom_themes_data = self.get()
     76         try:
     77             yield custom_themes_data
     78         except Exception:
     79             raise
     80         else:
     81             self.set(custom_themes_data)
     82 
     83     def is_equal(self, value):
     84         return False
     85 
     86     def _set(self, value):
     87         super(CustomColorThemesSetting, self)._set(value.serialize())
     88 
     89     def _validate(self, value):
     90         return isinstance(value, CustomColorThemeCollection)
     91 
     92 
     93 custom_color_themes = CustomColorThemesSetting()