Guest User

Untitled

a guest
Mar 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. module System.Taffybar.Widgets.StatusNotifierTray where
  2.  
  3. import Control.Monad
  4. import qualified Data.Vector as V
  5. import Data.Word
  6. import Foreign.C.Types (CUChar(..))
  7. import Foreign.Marshal.Array
  8. import Foreign.Ptr
  9. import Foreign.Storable
  10. import qualified Graphics.UI.Gtk as Gtk
  11. import StatusNotifier.Host.Service as H
  12.  
  13. vectorToCPointer :: V.Vector Word8 -> IO (Ptr CUChar)
  14. vectorToCPointer v = do
  15. target <- mallocArray $ V.length v
  16. V.imapM_ (pokeElemOff target) v
  17. return $ castPtr target
  18.  
  19. sampleBits :: Int
  20. sampleBits = 8
  21.  
  22. hasAlpha :: Bool
  23. hasAlpha = True
  24.  
  25. colorspace :: Gtk.Colorspace
  26. colorspace = Gtk.ColorspaceRgb
  27.  
  28. buildTray :: IO Gtk.Widget
  29. buildTray = do
  30. box <- Gtk.hBoxNew False 5
  31. let updateHandler NewItem info =
  32. do
  33. putStrLn "Handling new item"
  34. pixBuf <- getPixBufFromInfo info
  35. img <- Gtk.imageNew
  36. Gtk.imageSetFromPixbuf img pixBuf
  37. Gtk.widgetShowAll img
  38. Gtk.boxPackStart box img Gtk.PackNatural 0
  39. updateHandler _ _ = return ()
  40. getPixBufFromInfo ItemInfo { iconPixmaps = pixmaps } = do
  41. let (width, height, pixmap) = V.head pixmaps
  42. pixelsPerRow = fromIntegral width
  43. bytesPerPixel = 4
  44. rowStride = pixelsPerRow * bytesPerPixel
  45. cPtr <- vectorToCPointer pixmap
  46. Gtk.pixbufNewFromData cPtr colorspace hasAlpha sampleBits
  47. (fromIntegral width) (fromIntegral height) rowStride
  48. join $ H.build H.defaultParams { uniqueIdentifier = "taffybar"
  49. , handleUpdate = updateHandler
  50. }
  51. return $ Gtk.toWidget box
Add Comment
Please, Sign In to add comment