2024-03-14 10:19:35 +00:00
if ( ( require ( "Storage" ) . readJSON ( "messages.settings.json" , true ) || { } ) . maxMessages !== 0 ) {
// NOTE when adding a custom "messages" widget:
2022-12-08 21:33:04 +00:00
// the name still needs to be "messages": the library calls WIDGETS["messages'].hide()/show()
// see e.g. widmsggrid
2022-11-20 14:41:02 +00:00
WIDGETS [ "messages" ] = {
2023-04-25 21:09:05 +00:00
area : "tl" , width : 0 , srcs : [ ] , draw : function ( _w , recall ) {
2022-11-20 14:41:02 +00:00
// If we had a setTimeout queued from the last time we were called, remove it
if ( WIDGETS [ "messages" ] . i ) {
clearTimeout ( WIDGETS [ "messages" ] . i ) ;
delete WIDGETS [ "messages" ] . i ;
}
Bangle . removeListener ( "touch" , this . touch ) ;
if ( ! this . width ) return ;
let settings = Object . assign ( { flash : true , maxMessages : 3 } , require ( "Storage" ) . readJSON ( "messages.settings.json" , true ) || { } ) ;
if ( recall !== true || settings . flash ) {
2022-12-09 10:32:45 +00:00
const msgsShown = E . clip ( this . srcs . length , 0 , settings . maxMessages ) ;
2022-11-20 14:41:02 +00:00
g . reset ( ) . clearRect ( this . x , this . y , this . x + this . width , this . y + 23 ) ;
for ( let i = 0 ; i < msgsShown ; i ++ ) {
2022-12-19 10:17:08 +00:00
const src = this . srcs [ i ] ;
2022-11-20 14:41:02 +00:00
const colors = [
g . theme . bg ,
require ( "messageicons" ) . getColor ( src , { settings : settings } )
] ;
if ( settings . flash && ( ( Date . now ( ) / 1000 ) & 1 ) ) {
if ( colors [ 1 ] == g . theme . fg ) {
colors . reverse ( ) ;
} else {
colors [ 1 ] = g . theme . fg ;
}
}
g . setColor ( colors [ 1 ] ) . setBgColor ( colors [ 0 ] ) ;
// draw the icon, or '...' if too many messages
g . drawImage ( i == ( settings . maxMessages - 1 ) && this . srcs . length > settings . maxMessages ? atob ( "EASBAGGG88/zz2GG" ) : require ( "messageicons" ) . getImage ( src ) ,
this . x + 12 + i * 24 , this . y + 12 , { rotate : 0 /*force centering*/ } ) ;
}
}
2023-04-25 21:09:05 +00:00
WIDGETS [ "messages" ] . i = setTimeout ( ( ) => WIDGETS [ "messages" ] . draw ( WIDGETS [ "messages" ] , true ) , 1000 ) ;
2022-11-20 14:41:02 +00:00
if ( process . env . HWVERSION > 1 ) Bangle . on ( "touch" , this . touch ) ;
} , onMsg : function ( type , msg ) {
if ( this . hidden ) return ;
if ( type === "music" ) return ;
if ( msg . id && ! msg . new && msg . t !== "remove" ) return ;
2024-03-14 10:19:35 +00:00
let filterMessages = msgs => msgs . filter ( msg => msg . new && msg . id != "music" )
. filter ( ( msg , i , arr ) => arr . findIndex ( nmsg => msg . src == nmsg . src ) == i ) // only include one of each type
. map ( m => m . src ) ; // we only need this for icon/color;
2022-11-20 14:41:02 +00:00
this . srcs = filterMessages ( require ( "messages" ) . getMessages ( msg ) ) ;
const settings = Object . assign ( { maxMessages : 3 } , require ( 'Storage' ) . readJSON ( "messages.settings.json" , true ) || { } ) ;
this . width = 24 * E . clip ( this . srcs . length , 0 , settings . maxMessages ) ;
if ( type !== "init" ) Bangle . drawWidgets ( ) ; // "init" is not a real message type: see below
} , touch : function ( b , c ) {
var w = WIDGETS [ "messages" ] ;
if ( ! w || ! w . width || c . x < w . x || c . x > w . x + w . width || c . y < w . y || c . y > w . y + 24 ) return ;
require ( "messages" ) . openGUI ( ) ;
2022-12-08 21:33:04 +00:00
} ,
// hide() and show() are required by the "message" library!
hide ( ) {
2022-11-20 14:41:02 +00:00
this . hidden = true ;
if ( this . width ) {
// hide widget
this . width = 0 ;
Bangle . drawWidgets ( ) ;
}
} , show ( ) {
2024-03-14 10:19:35 +00:00
delete this . hidden ;
2022-11-20 14:41:02 +00:00
this . onMsg ( "show" , { } ) ; // reload messages+redraw
}
} ;
2022-12-14 22:03:33 +00:00
Bangle . on ( "message" , WIDGETS [ "messages" ] . onMsg . bind ( WIDGETS [ "messages" ] ) ) ;
2024-10-25 08:37:43 +00:00
if ( require ( "Storage" ) . read ( "messages.json" ) !== undefined ) // only call init if we've got messages - otherwise we can avoid loading messages lib (saves 30ms)
WIDGETS [ "messages" ] . onMsg ( "init" , { } ) ; // abuse type="init" to prevent Bangle.drawWidgets();
2024-03-14 10:19:35 +00:00
}