2017-08-01 2 views
5

J'ai récemment étudié les modèles de conception gtk, et trouvé le in-app notifications. Il y a une description sur quand l'utiliser, mais pas de référence à l'API gtk.gtk notifications dans l'application API référence

Je l'ai recherché, mais j'ai trouvé seulement les GNotification et GApplication.send_notification, mais cela envoie la notification à l'environnement de bureau.

Quelqu'un peut-il aider à trouver un tutoriel ou un exemple de code pour faire une notification dans l'application?

+0

Je voudrais utiliser moi-même :) J'ai donc pris la liberté de faire rapport aucune référence API pour le lien au bas de la page. – theGtknerd

+0

J'ai déjà envoyé un mail aujourd'hui, peut-être que ça ne prendra pas trop de temps :) – microo8

+0

microo8 @theGtknerd cochez la réponse. –

Répondre

5

Le "widget" de notification d'application est un mélange de widgets, d'une classe css et de comportements.

Vous devez utiliser un Gtk.Overlay dans la fenêtre que vous prévoyez d'utiliser les notifications d'application, puis utiliser un Gtk.Frame avec la classe app-notification prédéfinie. Le Gtk.Frame doit être enveloppé dans un Gtk.Revealer pour permettre la transition "slide".

Voici un fichier ui Glade (app-notification.ui) avec un exemple:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.20.0 --> 
<interface> 
    <requires lib="gtk+" version="3.20"/> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="default_width">440</property> 
    <property name="default_height">250</property> 
    <child> 
     <object class="GtkOverlay" id="overlay1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <child> 
      <object class="GtkBox" id="box1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="orientation">vertical</property> 
      <child> 
       <object class="GtkLabel" id="label1"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">APP-NOTIFICATION EXAMPLE</property> 
       </object> 
       <packing> 
       <property name="expand">True</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="button1"> 
       <property name="label" translatable="yes">show app-notification</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="index">-1</property> 
      </packing> 
     </child> 
     <child type="overlay"> 
      <object class="GtkOverlay" id="overlay2"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="valign">start</property> 
      <child> 
       <object class="GtkRevealer" id="revealer2"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="halign">center</property> 
       <child> 
        <object class="GtkFrame" id="frame2"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <property name="label_xalign">0</property> 
        <property name="shadow_type">none</property> 
        <child> 
         <object class="GtkBox" id="box2"> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="spacing">20</property> 
         <child> 
          <object class="GtkLabel" id="label2"> 
          <property name="visible">True</property> 
          <property name="can_focus">False</property> 
          <property name="label" translatable="yes">This is an app-notification. Click the button to dismiss</property> 
          </object> 
          <packing> 
          <property name="expand">False</property> 
          <property name="fill">True</property> 
          <property name="position">0</property> 
          </packing> 
         </child> 
         <child> 
          <object class="GtkButton" id="button2"> 
          <property name="visible">True</property> 
          <property name="can_focus">True</property> 
          <property name="receives_default">True</property> 
          <property name="relief">none</property> 
          <child> 
           <object class="GtkImage" id="image2"> 
           <property name="visible">True</property> 
           <property name="can_focus">False</property> 
           <property name="icon_name">window-close-symbolic</property> 
           </object> 
          </child> 
          <style> 
           <class name="image-button"/> 
          </style> 
          </object> 
          <packing> 
          <property name="expand">False</property> 
          <property name="fill">True</property> 
          <property name="position">1</property> 
          </packing> 
         </child> 
         </object> 
        </child> 
        <child type="label_item"> 
         <placeholder/> 
        </child> 
        <style> 
         <class name="app-notification"/> 
        </style> 
        </object> 
       </child> 
       </object> 
       <packing> 
       <property name="pass_through">True</property> 
       <property name="index">-1</property> 
       </packing> 
      </child> 
      </object> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

Le résultat Glade:

enter image description here

Et voici un code python qui utilise le fichier glade précédent et donne un certain comportement dynamique à la notification afin que vous puissiez le voir en action en cliquant sur les boutons. Le fichier doit être nommé Glade app-notification.ui, sinon changer le code pour refléter le nom donné:

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 

def onButtonShow(self): 
    revealer.set_reveal_child(True) 

def onButtonClose(self): 
    revealer.set_reveal_child(False) 

builder = Gtk.Builder() 
builder.add_from_file("app-notification.ui") 

window = builder.get_object("window1") 

buttonShow = builder.get_object("button1") 
buttonClose = builder.get_object ("button2") 
revealer = builder.get_object("revealer2") 

buttonShow.connect ("clicked", onButtonShow) 
buttonClose.connect ("clicked", onButtonClose) 
window.connect ("destroy", Gtk.main_quit) 
window.show_all() 

Gtk.main() 
+1

Par curiosité, où avez-vous trouvé cette information? – theGtknerd

+0

@theGtknerd pour dire la vérité je ne me souviens pas vraiment d'où exactement mais j'ai saisi des fichiers CSS pour rechercher des noms de classes. Quelque chose comme une recherche cumulative, Get A, avec A obtenu à B ... puis à C. Une bonne approche est de se souvenir d'une application qui utilise quelque chose que vous voulez alors aller aux sources à github ou gnome git et essayer de trouver comment ils l'ont mis en œuvre. De toute façon vous avez tous les deux raison. Il n'y a pas de documentation sur la façon de l'atteindre. peut-être devrions-nous ajouter quelque chose comme ça au Gnome/HowforI –