如何处理多个常见用户界面?

发布于 2024-08-17 06:36:37 字数 574 浏览 7 评论 0原文

我正在开发一个在两个不同平台上运行的 python 应用程序,即常规桌面 linux 和 Maemo 4。我们在这两个平台上都使用 PyGTK,但在 Maemo 上有很多小调整,使其看起来不错,具体实现如下:

if util.platform.MAEMO:
    # do something fancy for maemo
else:
    # regular pygtk

大约需要 15 个 if 语句才能使 UI 在 Maemo 4 上看起来良好并正常工作。

这一直以来都非常易于管理。问题是,不久前发布了 Maemo 的新版本(5,又名 fremantle),与 Maemo 4 相比,它有一些很大的差异。我不想在整个 GUI 代码中添加一堆检查,以便让所有 3 个平台都使用相同的代码库很好地工作,因为那样会变得混乱。我也不想为每个平台创建原始 GUI 代码的副本,并简单地针对特定平台对其进行修改(我想重复使用尽可能多的代码)。

那么,有什么方法可以让基于相同核心 UI 代码的不同平台拥有稍微不同的 UI 呢?我不认为这是一个 python 或 Maemo 特定的问题,我只是想知道这是如何完成的。

I'm working on a python application that runs on 2 different platforms, namely regular desktop linux and Maemo 4. We use PyGTK on both platforms but on Maemo there are a bunch of little tweaks to make it look nice which are implemented as follows:

if util.platform.MAEMO:
    # do something fancy for maemo
else:
    # regular pygtk

There are roughly 15 of these if statements needed to get the UI looking and working nice on Maemo 4.

This has been very manageable for all this time. The problem is that a while ago there was a new version of Maemo released (5, aka fremantle) and it has some big differences compared to Maemo 4. I don't want to add a bunch of checks throughout the GUI code in order to get all 3 platforms working nicely with the same codebase because that would get messy. I also don't want to create a copy of the original GUI code for each platform and simply modify it for the specific platform (I'd like to re-use as much code as possible).

So, what are ways to have slightly different UIs for different platforms which are based on the same core UI code? I don't think this is a python or Maemo-specific question, I'd just like to know how this is done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-08-24 06:36:37

您可以在工厂中完成大部分工作:

def createSpec():
  if util.platform.MAEMO: return Maemo4Spec()
  elif util.platform.MAEMO5: return Maemo5Spec()
  return StandardPyGTKSpec()

然后,在代码的早期某个位置,您只需调用该工厂:

 spec = createSpec()

现在,在其他任何有条件的地方,您只需调用必要的函数:

 spec.drawComboBox()

只要 drawComboBox()< /code>,处理特定于平台的任何事情,你应该处于良好状态。

You could wind up much of this in a factory:

def createSpec():
  if util.platform.MAEMO: return Maemo4Spec()
  elif util.platform.MAEMO5: return Maemo5Spec()
  return StandardPyGTKSpec()

Then, somewhere early in your code, you just call that factory:

 spec = createSpec()

Now, everywhere else you had conditions, you just call the necessary function:

 spec.drawComboBox()

As long as drawComboBox(), handles anything specific to the platform, you should be in good shape.

你是暖光i 2024-08-24 06:36:37

您可以将需要执行的特定于平台的操作隔离为 platform 模块内一致命名的小函数,使用您正在运行的平台创建正确的函数名称,然后 getattr 正确的一个并调用它。 if/else 样板就会消失。

You could isolate the platform specific stuff you need to do into small consistently named functions inside a platform module, create the right function name using the platform you're running on and then getattr the right one and call it. The if/else boilerplate would disappear then.

落墨 2024-08-24 06:36:37

我制作了一个单独的模块来处理普通 Linux、Maemo 4.1 和 Maemo 5 之间的所有专业知识。它检测可用的功能并允许程序正常降级。

例如

 def _fremantle_hildonize_window(app, window):
         oldWindow = window
         newWindow = hildon.StackableWindow()
         oldWindow.get_child().reparent(newWindow)
         app.add_window(newWindow)
         return newWindow


 def _hildon_hildonize_window(app, window):
         oldWindow = window
         newWindow = hildon.Window()
         oldWindow.get_child().reparent(newWindow)
         app.add_window(newWindow)
         return newWindow


 def _null_hildonize_window(app, window):
         return window


 try:
         hildon.StackableWindow
         hildonize_window = _fremantle_hildonize_window
 except AttributeError:
         try:
                 hildon.Window
                 hildonize_window = _hildon_hildonize_window
         except AttributeError:
                 hildonize_window = _null_hildonize_window

,更多信息请参见
Dialcentral、Gonert、ejpi 或 Quicknote 名为 hildonize.py 的文件的源代码
https ://garage.maemo.org/plugins/ggit/browse.php/?p=gc-dialer;a=blob;f=src/hildonize.py;

The One Ring 的 GObject Utils 的另一个示例 (go_utils.py)

 def _old_timeout_add_seconds(timeout, callback):
         return gobject.timeout_add(timeout * 1000, callback)


 def _timeout_add_seconds(timeout, callback):
         return gobject.timeout_add_seconds(timeout, callback)


 try:
         gobject.timeout_add_seconds
         timeout_add_seconds = _timeout_add_seconds
 except AttributeError:
         timeout_add_seconds = _old_timeout_add_seconds

I've made a separate module to handle all of my specializing between normal Linux, Maemo 4.1, and Maemo 5. It detects what features are available and allows the program to gracefully degrade.

For example

 def _fremantle_hildonize_window(app, window):
         oldWindow = window
         newWindow = hildon.StackableWindow()
         oldWindow.get_child().reparent(newWindow)
         app.add_window(newWindow)
         return newWindow


 def _hildon_hildonize_window(app, window):
         oldWindow = window
         newWindow = hildon.Window()
         oldWindow.get_child().reparent(newWindow)
         app.add_window(newWindow)
         return newWindow


 def _null_hildonize_window(app, window):
         return window


 try:
         hildon.StackableWindow
         hildonize_window = _fremantle_hildonize_window
 except AttributeError:
         try:
                 hildon.Window
                 hildonize_window = _hildon_hildonize_window
         except AttributeError:
                 hildonize_window = _null_hildonize_window

For more, see
Dialcentral, Gonert, ejpi, or Quicknote's source code for a file called hildonize.py
https://garage.maemo.org/plugins/ggit/browse.php/?p=gc-dialer;a=blob;f=src/hildonize.py;

Another example from The One Ring's GObject Utils (go_utils.py)

 def _old_timeout_add_seconds(timeout, callback):
         return gobject.timeout_add(timeout * 1000, callback)


 def _timeout_add_seconds(timeout, callback):
         return gobject.timeout_add_seconds(timeout, callback)


 try:
         gobject.timeout_add_seconds
         timeout_add_seconds = _timeout_add_seconds
 except AttributeError:
         timeout_add_seconds = _old_timeout_add_seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文